我们可以使用什么unix命令将某些文本附加到文件中的特定行?

Ale*_*ong 1 unix editing file line

有没有人知道一系列unix命令允许某人将某些文本附加到文件中特定行的末尾?

例如

第1行

第2行

第3行

第4行

我希望将文本",额外信息"附加到第3行,以便文件现在看起来像这样:

第1行

第2行

第3行,额外信息

第4行

sfo*_*sen 8

在awk它是:

awk '{s=$0; if( NR==3 ){ s=s ", Extra Information" } print s;}' myfile > newfile
Run Code Online (Sandbox Code Playgroud)

适当的sed版本:

sed -e '3s/$/, Extra Information/' -i myfile
Run Code Online (Sandbox Code Playgroud)


mou*_*iel 5

这是一个带便携版本sed(没有-i选项):

sed '3s/$/Hello World/' myfile
Run Code Online (Sandbox Code Playgroud)

请注意,myfile未修改,因此您可以从可能的错误中恢复.