我想在file的特定行的末尾添加一个变量。到目前为止,我的代码: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
这是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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句