linux 中如何在文件的行首或者行尾添加一行

 

在文件的行首添加一行。

001、 echo 实现

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt                   ## 测试文件
a
b
c
[root@PC1 test2]# echo "xxx" | cat - a.txt    ## echo实现
xxx
a
b
c

 

 

002、sed实现

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt
a
b
c
[root@PC1 test2]# sed '1i xxx' a.txt  
xxx
a
b
c

 

 

003、awk实现

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt
a
b
c
[root@PC1 test2]# awk 'BEGIN{print "xxxx"} {print $0}' a.txt
xxxx
a
b
c

 

 

b、在文件的末尾添加一行

001、echo实现

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt
a
b
c
[root@PC1 test2]# echo "xxx" >> a.txt   
[root@PC1 test2]# cat a.txt
a
b
c
xxx

 

 

002、sed实现

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt
a
b
c
[root@PC1 test2]# sed '$a xxx' a.txt
a
b
c
xxx

 

 

003、awk实现

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt
a
b
c
[root@PC1 test2]# awk '{print $0} END {print "xxxx"}' a.txt
a
b
c
xxxx

 

posted @ 2022-09-26 11:06  小鲨鱼2018  阅读(6194)  评论(0编辑  收藏  举报