在特定行的末尾将变量写入文件

retep

我想在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
桑多·冈贝(SándorGombai)

这是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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章