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 @   小鲨鱼2018  阅读(7174)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-09-26 sratoolkits软件的安装 cdb-config:command not found 解决方法
2020-09-26 R语言中order函数的用法,对数据框进行排序
2020-09-26 linux系统 comm命令
2020-09-26 《Linux就该这么学》学习笔记 04
点击右上角即可分享
微信分享提示