特定の行の末尾に変数をファイルに書き込む

特定の行の末尾に変数をファイルに書き込む

変数を追加したい終わり特定のファイル内の行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

関連情報