在文件中添加文本

Shw*_*eta 4 linux shell

我使用awk命令在文件中查找特定行,并希望将其添加到第二个文件中.有人可以在这方面帮助我吗?

Dae*_*yth 11

简短的回答是,你做不到.你需要一个临时文件.

echo "Prepended Line" > tmpfile && cat origfile >> tmpfile && mv tmpfile origfile
Run Code Online (Sandbox Code Playgroud)

编辑:

sed -i 's/\(line you want\)/Prefix \1/g' origfile
Run Code Online (Sandbox Code Playgroud)

  • +1另外:`sed -i'/ line你想要/ s/^ /前缀/ g'ovnfile`或`sed -i'1s/^ /前缀/ g'origfile`(后者基于OP的自我回答) (3认同)