
我想添加一個變數結尾的一個具體的文件中的行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