sed:过滤、转换文本的流编辑器,sed支持管道命令,可以分析标准输入(standard input)。每次从输入中读取一行,进行相关的处理,将生成的数据输出到STDOUT,重复下一行直到读完所有的数据行。友情链接:正则表达式的初探
命令格式:$sed [-nefri] '动作行为' file_name
常用参数及说明:
- -n:silent模式。默认所有来自STDIN的数据都会显示在STDOUT,但是加上-n,只有经过sed处理的行才会被列出来
- -e:直接在指令列模式上进行sed的动作编辑
- -f:直接将sed的动作写在一个文档里面,-f file_name 则可以执行file_name内的sed动作
- -r:支持扩展正则表达式,默认不支持
- -i:直接修改file_name,不输出到STDOUT
动作说明:[n1[,n2] 动作行为# 动作行为要用单引号
n1,n2表示要操作的行数,例如,在10,20行之间进行。10,20[动作行为] $可以表示最后一行。例如1,$表示第一行到最后一行
动作行为:
- a: 新增,a的后面可以接strings,这些strings会在新的一行出现(目前的下一行)
- c:取代,c的后面可以接strings,这些strings可以取代n1,n2之间的行
- d:删除,删除前面的行数
- i:插入,i的后面可以接strings,这些strings会在新的一行出现(目前的上一行)
- p:打印,选择某行打印
- s:取代,e.g. 1,20s/old/new/g 在1行到20行用new字符串取代old字符串
例子:
[root@rhel6164 sed]# cat test.txt #原始文本
this is the first line
this is the second line
this is the third line
[root@rhel6164 sed]# sed '2a this is sed line' ./test.txt #在第二行后面增加一行
this is the first line
this is the second line
this is sed line
this is the third line
[root@rhel6164 sed]# sed '1,2c this is sed line' ./test.txt #把第一行和第二行替换成新的一行
this is sed line
this is the third line
[root@rhel6164 sed]# sed '1d' ./test.txt #删除第一行
this is the second line
this is the third line
[root@rhel6164 sed]# sed '2i this is sed line' ./test.txt #在第二行之前插入新的一行
this is the first line
this is sed line
this is the second line
this is the third line
[root@rhel6164 sed]# sed '1,$p' ./test.txt #打印所有的行,因为没有加-n参数,所以sed把原始文本和处理的文本都打印出来(共打印了两次)
this is the first line
this is the first line
this is the second line
this is the second line
this is the third line
this is the third line
[root@rhel6164 sed]# sed -n '1,$p' ./test.txt #只打印处理过的行
this is the first line
this is the second line
this is the third line
[root@rhel6164 sed]# sed '1,$s/this/The/g' ./test.txt #用The替换文档中所有的this
The is the first line
The is the second line
The is the third line
[root@rhel6164 sed]# sed '1,$s/this is//g' ./test.txt #用空格替换this is
the first line
the second line
the third line
[root@rhel6164 sed]# sed -e 's/first/FIRST/g;s/second/SECOND/g;s/third/THIRD/g' ./test.txt
# 一次执行多个动作,每个动作用分号隔开,可以用-e或者不用也可以
this is the FIRST line
this is the SECOND line
this is the THIRD line
[root@rhel6164 sed]# cat rule
s/first/FIRST/g
s/second/SECOND/g
s/third/THIRD/g
[root@rhel6164 sed]# sed -f rule ./test.txt
# 把执行动作写入一个文件
this is the FIRST line
this is the SECOND line
this is the THIRD line
新的原始文件
[root@rhel6164 sed]# cat test.txt
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fifth line
[root@rhel6164 sed]# cat test.txt | sed -n '2,/forth/p' #打印从第二行开始直到匹配到forth字符串为止
this is the second line
this is the third line
this is the forth line
[root@rhel6164 sed]# cat test.txt | sed -n '/second/,$p' #打印从匹配到second字符串开始知道文本末尾
this is the second line
this is the third line
this is the forth line
this is the fifth line
打印匹配到两行字符串的中间所有行
[root@rhel6164 sed]# cat test.txt | sed -n '/second/,$p' | sed -n '1,/forth/p' #打印second和forth字符串中间的所有行
this is the second line
this is the third line
this is the forth line
sed与正则表达式
[root@rhel6164 sed]# sed -n '/first/p' ./test.txt #两个斜杠/(slash)里面可以放正则表达式,这里打印匹配到first字符串的行
this is the first line
[root@rhel6164 sed]# sed -nr '/(first|fifth)/p' ./test.txt #sed默认是不支持扩展正则表达式,这里使用了扩展正则表达式,要使用-r选项,这里打印包含first或者fifth字符串的行
this is the first line
this is the fifth line
删除行首的空格(包括空格和制表符TAB)
[root@rhel6164 sed]# cat test.txt
this is the first line
this is the second line
[root@rhel6164 sed]# sed 's/^[[:blank:]]*//g' ./test.txt #删除行首的空格或者制表符TAB
this is the first line
this is the second line