AWK - 如何添加文件的第一行?

时间:2014-06-26 07:14:11

标签: awk gawk

此命令将行附加到文件末尾

  

for $ in $(ls);做awk'BEGIN {print“last_line”}'>> “$ I”;完成

如何在文件开头添加行?我试过了

  

for $ in $(ls);做awk'NR == 1 {print“first_line”}'1“$ i”> “$ I”;完成

2 个答案:

答案 0 :(得分:0)

改用sed

[rdarji@indlin091 Experiments]$ cat File2
This is line number: 2
This is line number: 3
This is line number: 4
This is line number: 5
[rdarji@indlin091 Experiments]$ sed -i '1i\
> This is line number: 1' File2
[rdarji@indlin091 Experiments]$ cat File2
This is line number: 1
This is line number: 2
This is line number: 3
This is line number: 4
This is line number: 5

答案 1 :(得分:0)

sed答案有效。如果你喜欢awk:

awk 'NR==1{$0="first line"RS$0}7' file > tmp && mv tmp file

你可以把它放在你的循环中。