Linux三剑客:grep、sed、awk。grep主打查找功能,sed主要是编辑行,awk主要是分割列处理。本篇文章我们详细介绍sed命令。
sed 是stream editor的简称,也就是流编辑器。它是文本处理中非常重要的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
[root@liuchao ~]# sed --help
用法: sed [选项] '命令' [输入文件]
# 要查找的文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4
# 将文件内的所有‘p’字符替换为'd' 字符 将替换结果打印到屏幕上
[root@liuchao sed_test]# sed 'y/p/d/' sedtest_01.txt
this is a dage1
this is a dage2
this is a dage3
this is a dage4
# 源文件其实是未替换的
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4
[root@liuchao sed_test]#
# 目标字符长度和源字符长度不一致 报错
[root@liuchao sed_test]# sed 'y/page/dd/' sedtest_01.txt
sed:-e 表达式 #1,字符 10:“y”命令的字符串长度不同
# 查找文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4
# s和y 功能相识,唯一有区别的是目标字符长度和源字符长度可以不一致
[root@liuchao sed_test]# sed 's/pa/ddd/' sedtest_01.txt
this is a dddge1
this is a dddge2
this is a dddge3
this is a dddge4
[root@liuchao sed_test]#
# 测试文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1 Page5 page6
test is a page10
this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9
# 删除所有空白行
[root@liuchao sed_test]# sed '/^$/d' sedtest_01.txt
this is a page1 Page5 page6
test is a page10
this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9
# 删除第二行
[root@liuchao sed_test]# sed '2d' sedtest_01.txt
this is a page1 Page5 page6
this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9
# 删除第二行至最后一行
[root@liuchao sed_test]# sed '2,$d' sedtest_01.txt
this is a page1 Page5 page6
# 删除最后一行
[root@liuchao sed_test]# sed '$d' sedtest_01.txt
this is a page1 Page5 page6
test is a page10
this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9
# 删除所有以 ‘test’ 开头的行
[root@liuchao sed_test]# sed '/^test/'d sedtest_01.txt
this is a page1 Page5 page6
this is a page2
this is a page3
this is a page4
this is a Page7
[root@liuchao sed_test]#
# 测试文件内容
[root@liuchao sed_test]# cat sedtest_2.txt
this is a page1 Page5 page6
this is a page2
this is a page3
this is a page4
this is a Page7
[root@liuchao sed_test]#
# 打印所有行 可以看出所有行打印了两边
[root@liuchao sed_test]# sed p sedtest_2.txt
this is a page1 Page5 page6
this is a page1 Page5 page6
this is a page2
this is a page2
this is a page3
this is a page3
this is a page4
this is a page4
this is a Page7
this is a Page7
# 将匹配出的结果打印一遍
[root@liuchao sed_test]# sed '/page2/p' sedtest_2.txt
this is a page1 Page5 page6
this is a page2
this is a page2
this is a page3
this is a page4
this is a Page7
[root@liuchao sed_test]#
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4
[root@liuchao sed_test]# sed = sedtest_01.txt
1
this is a page1
2
this is a page2
3
this is a page3
4
this is a page4
[root@liuchao sed_test]#
# 测试文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1 page5 page6
this is a page2
this is a page3
this is a page4
# 通过 s g 查找所有匹配的字符串 并打印再屏幕上 注意:源文件内容未被更改
[root@liuchao sed_test]# sed 's/pa/dd/g' sedtest_01.txt
this is a ddge1 ddge5 ddge6
this is a ddge2
this is a ddge3
this is a ddge4
# 再次查看源文件内容,源文件未被更改
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1 page5 page6
this is a page2
this is a page3
this is a page4
# 通过 s g 查找所有匹配的字符串 并更改源文件内容
[root@liuchao sed_test]# sed -i 's/pa/dd/g' sedtest_01.txt
[root@liuchao sed_test]#
# 可以看出源文件内容被更改
[root@liuchao sed_test]# cat sedtest_01.txt
this is a ddge1 ddge5 ddge6
this is a ddge2
this is a ddge3
this is a ddge4
[root@liuchao sed_test]#
加上g标识,将会替换行所有匹配的字符串
# 查找的文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1 page5 page6
this is a page2
this is a page3
this is a page4
[root@liuchao sed_test]#
# 通过s 查找每行第一个匹配的
[root@liuchao sed_test]# sed 's/pa/dd/' sedtest_01.txt
this is a ddge1 page5 page6
this is a ddge2
this is a ddge3
this is a ddge4
# 通过 s g 组合查找每行所有匹配
[root@liuchao sed_test]# sed 's/pa/dd/g' sedtest_01.txt
this is a ddge1 ddge5 ddge6
this is a ddge2
this is a ddge3
this is a ddge4
[root@liuchao sed_test]#
# 查找文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1 Page5 page6
this is a page2
this is a page3
this is a page4
this is a Page7
[root@liuchao sed_test]#
# 通过 s i 查找每行第一个匹配的字符串 并替换打印在屏幕
[root@liuchao sed_test]# sed 's/pa/dd/i' sedtest_01.txt
this is a ddge1 Page5 page6
this is a ddge2
this is a ddge3
this is a ddge4
this is a ddge7
[root@liuchao sed_test]#
# 通过 s ig 标志位组合方式查找每行所有匹配的字符串,并替换打印在屏幕
[root@liuchao sed_test]# sed 's/pa/dd/ig' sedtest_01.txt
this is a ddge1 ddge5 ddge6
this is a ddge2
this is a ddge3
this is a ddge4
this is a ddge7
[root@liuchao sed_test]#