标签:添加 code rip version div com 工作 tmp 编写
Linux sed 命令是利用脚本来处理文本文件。
sed 可依照脚本的指令来处理、编辑文本文件。
Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。
1. sed -e 1a\添加一行 /root/tmp/words.txt
[root@Server-n93yom tmp]# nl words.txt 1 the day is sunny the the 2 hello java 3 he sunny is is
[root@Server-n93yom ~]# sed -e 1a\添加一行 /root/tmp/words.txt the day is sunny the the 添加一行 hello java he sunny is is
1a意思为在第一行后面添加一行 不是真实添加
2.nl -b a words.txt | sed ‘2d‘ 删除第二行
[root@Server-n93yom tmp]# nl -b a words.txt 1 the day is sunny the the 2 3 hello java 4 he sunny is is [root@Server-n93yom tmp]# nl -b a words.txt | sed ‘2d‘ 1 the day is sunny the the 3 hello java 4 he sunny is is
3. nl -b a words.txt | sed ‘2,$d‘ 从第二行删除到最后一行
[root@Server-n93yom tmp]# nl -b a words.txt | sed ‘2d‘ 1 the day is sunny the the 3 hello java 4 he sunny is is [root@Server-n93yom tmp]# nl -b a words.txt | sed ‘2,$d‘ 1 the day is sunny the the
4.nl -b a words.txt | sed ‘2a test123‘ 在第二行后添加 test123
[root@Server-n93yom tmp]# nl -b a words.txt | sed ‘2a test123‘ 1 the day is sunny the the 2 test123 3 hello java 4 he sunny is is
5.nl words.txt | sed ‘1,4c haha‘ 第1到第4行,用haha替代
[root@Server-n93yom tmp]# nl words.txt | sed ‘1,4c haha‘ haha
6.nl words.txt | sed -n ‘3,4p‘ 打印第3和第4行数据
[root@Server-n93yom tmp]# nl words.txt | sed -n ‘3,4p‘ 2 hello java 3 he sunny is is
7.nl words.txt | sed -n ‘/hello/p‘ 打印只包含hello的行
[root@Server-n93yom tmp]# nl words.txt | sed -n ‘/hello/p‘ 2 hello java
8.nl words.txt | sed ‘/hello/d‘ 删除hello的匹配行,并打印剩下的行
[root@Server-n93yom tmp]# nl words.txt | sed ‘/hello/d‘ 1 the day is sunny the the 3 he sunny is is
9.nl words.txt | sed ‘s/hello/guan/g‘ 搜索hello并替换为guan sed ‘s/要被取代的字串/新的字串/g‘
[root@Server-n93yom tmp]# nl words.txt | sed ‘s/hello/guan/g‘ 1 the day is sunny the the 2 guan java 3 he sunny is is
10. nl words.txt | grep hello | sed ‘s/java//g‘ 找出hello的行,并把后面的java去除
[root@Server-n93yom tmp]# nl words.txt | grep hello | sed ‘s/java//g‘ 2 hello
11.sed -i ‘$a # This is a test‘ regular_express.txt -i会真正的对文件进行编辑,所以对于系统文件要谨慎使用
[root@Server-n93yom tmp]# sed -i ‘$a # This is a test‘ words.txt [root@Server-n93yom tmp]# nl words.txt 1 the day is sunny the the 2 hello java 3 he sunny is is 4 # This is a test
sed 的 -i 选项可以直接修改文件内容,这功能非常有帮助!举例来说,如果你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!因为文件太大了
以上都是自己手敲一遍的,如若有误请指正,谢谢
参考:https://www.runoob.com/linux/linux-comm-sed.html
标签:添加 code rip version div com 工作 tmp 编写
原文地址:https://www.cnblogs.com/guanbin-529/p/11441009.html