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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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