특정 줄의 끝 부분에 있는 파일에 변수를 씁니다.

특정 줄의 끝 부분에 있는 파일에 변수를 씁니다.

변수를 추가하고 싶습니다.~의특정한파일의 줄 file.txt. 지금까지 내 코드는 다음과 같습니다.

#!/bin/bash    
read -p "What is the path of the repo? > " input
echo :$input  >> file.txt

에 추가하고 싶어요~의2호선. 예를 들어,

file.txt
--------
before -
1.stuff 
2./home/retep/awesome

after -
1.stuff
2./home/retep/awesome:/home/retep/cool

답변1

awk에 대한 또 다른 단일 라이너입니다.

#!/bin/bash    
read -p "What is the path of the repo? > " input

awk -v addition="$input" -v targetline=2 '
NR==targetline { print $0 ":" addition; next}
{print }' file.txt > file.tmp
mv file.tmp file.txt

관련 정보