处理文本的工具sed
stream EDitor,行编辑器
sed是一种流编辑器,他一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,成为“模式空间”(pattern space),接着用sed命令处理缓存区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,知道文本末尾。文件内容并没有改变,除非你使用重定向存储输出。sed主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。
sed option… 'script' inputfile
-n:不输出模式空间内容的自动打印
-e:多点编辑
-f /path/to/script_file:从指定文件中读取编辑脚本
-r:支持使用扩展正则表达式
-i:原处编辑
script:
‘地址命令’
地址定界:
不给地址:对全文进行处理
单地址:
n:指定的行
/pattern/:被此处模式所能够匹配到的每一行
地址范围:
n,m:n到m行
n,+m:n到n+m行
/pat1/,/pat2/
n,/pat1/
~:步进
1~2 奇数行
2~2 偶数行
编辑命令:
d:删除模式空间匹配的行
p:显示模式空间中的内容
a \text:在行后面追加文本,支持使用\n实现多行追加
i \text:在行前面插入文本,支持使用\n实现多行插入
c \text:替换行为单行或多行文本
w /path/to/somefile:保存模式匹配的行至指定文件
r /path/from/somefile:读取指定文件的文本至模式空间中匹配到的行后
=:为模式空间中的行打印行号
!:模式空间中匹配行取反处理
[root@localhost wang]# cat file3 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3 inet 127.0.0.1/8 scope host lo 4 valid_lft forever preferred_lft forever 5 inet6 ::1/128 scope host 6 valid_lft forever preferred_lft forever 7 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 8 link/ether 00:0c:29:54:19:1f brd ff:ff:ff:ff:ff:ff 9 inet 192.168.1.114/24 brd 192.168.1.255 scope global dynamic eno16777736 10 valid_lft 6320sec preferred_lft 6320sec 11 inet6 fe80::20c:29ff:fe54:191f/64 scope link 12 valid_lft forever preferred_lft forever [root@localhost wang]# sed '2p' file3 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3 inet 127.0.0.1/8 scope host lo 4 valid_lft forever preferred_lft forever 5 inet6 ::1/128 scope host 6 valid_lft forever preferred_lft forever 7 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 8 link/ether 00:0c:29:54:19:1f brd ff:ff:ff:ff:ff:ff 9 inet 192.168.1.114/24 brd 192.168.1.255 scope global dynamic eno16777736 10 valid_lft 6320sec preferred_lft 6320sec 11 inet6 fe80::20c:29ff:fe54:191f/64 scope link 12 valid_lft forever preferred_lft forever [root@localhost wang]# sed -n '2p' file3 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 [root@localhost wang]# sed -n '1,4p' file3 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3 inet 127.0.0.1/8 scope host lo 4 valid_lft forever preferred_lft forever [root@localhost wang]# sed -n '/inet/p' file3 3 inet 127.0.0.1/8 scope host lo 5 inet6 ::1/128 scope host 9 inet 192.168.1.114/24 brd 192.168.1.255 scope global dynamic eno16777736 11 inet6 fe80::20c:29ff:fe54:191f/64 scope link [root@localhost wang]# sed -n '1,/inet/p' file3 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3 inet 127.0.0.1/8 scope host lo 注:默认到/pattern/第一个 [root@localhost wang]# sed '/inet/a\gebilaowang' file3 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3 inet 127.0.0.1/8 scope host lo gebilaowang 4 valid_lft forever preferred_lft forever 5 inet6 ::1/128 scope host gebilaowang 6 valid_lft forever preferred_lft forever 7 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 8 link/ether 00:0c:29:54:19:1f brd ff:ff:ff:ff:ff:ff 9 inet 192.168.1.114/24 brd 192.168.1.255 scope global dynamic eno16777736 gebilaowang 10 valid_lft 6320sec preferred_lft 6320sec 11 inet6 fe80::20c:29ff:fe54:191f/64 scope link gebilaowang 12 valid_lft forever preferred_lft forever 注:在/pattern/行后添加gebilaowang [root@localhost wang]# sed '/inet/i\gebilaowang' file3 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 gebilaowang 3 inet 127.0.0.1/8 scope host lo 4 valid_lft forever preferred_lft forever gebilaowang 5 inet6 ::1/128 scope host 6 valid_lft forever preferred_lft forever 7 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 8 link/ether 00:0c:29:54:19:1f brd ff:ff:ff:ff:ff:ff gebilaowang 9 inet 192.168.1.114/24 brd 192.168.1.255 scope global dynamic eno16777736 10 valid_lft 6320sec preferred_lft 6320sec gebilaowang 11 inet6 fe80::20c:29ff:fe54:191f/64 scope link 12 valid_lft forever preferred_lft forever 注:在/pattern/行前添加gebilaowang [root@localhost wang]# sed '/inet/c\gebilaowang' file3 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 2 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 gebilaowang 4 valid_lft forever preferred_lft forever gebilaowang 6 valid_lft forever preferred_lft forever 7 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 8 link/ether 00:0c:29:54:19:1f brd ff:ff:ff:ff:ff:ff gebilaowang 10 valid_lft 6320sec preferred_lft 6320sec gebilaowang 12 valid_lft forever preferred_lft forever 注:gebilaowang代替了/pattern/行的内容 [root@localhost wang]# sed '1,10d' file3 11 inet6 fe80::20c:29ff:fe54:191f/64 scope link 12 valid_lft forever preferred_lft forever [root@localhost wang]# nl file3 | sed '2,5d' 1 1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 6 6 valid_lft forever preferred_lft forever 7 7 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 8 8 link/ether 00:0c:29:54:19:1f brd ff:ff:ff:ff:ff:ff 9 9 inet 192.168.1.114/24 brd 192.168.1.255 scope global dynamic eno16777736 10 10 valid_lft 4557sec preferred_lft 4557sec 11 11 inet6 fe80::20c:29ff:fe54:191f/64 scope link 12 12 valid_lft forever preferred_lft forever
[root@localhost wang]# cat -n file2 1 123123 2 3 123412 4 4124 5 31 6 7 8 12413 9 4 [root@localhost wang]# sed -n '/^$/=' file2 2 6 7 注:显示空行行号,空格非空行。 [root@localhost wang]# cat -n file2 1 2 a 3 4 adas 5 6 qweqwrq [root@localhost wang]# sed '/^$/d' file2 a adas qweqwrq 注:删除空行,空格不是空行
s///:查找替换,支持使用其他分隔符,s@@@,s###
替换标记:
g:行内全局替换
p:显示替换成功的行
w /path/to/somefile:将替换成功的行保存至文件中
高级编辑命令
h:把模式空间中的内容覆盖至保持空间中
H:把模式空间中的内容追加至保持空间中
g:从保持空间取出数据覆盖至模式空间
G:从保持空间取出内容追加只模式空间
x:把模式空间中的内容与保持空间中的内容进行互换
n:读取匹配到的行的下一行覆盖至模式空间
N:追加匹配到的行的下一行只模式空间
d:删除模式空间中的行
D:删除当前模式空间开端至\n的内容(不在传至标准输出)。放弃之后的命令,但是对剩余模式空间重新执行sed
(C|c)at:Cat或cat
[root@localhost wang]# cat file3 1 2 3 4 5 6 7 8 9 10 [root@localhost wang]# sed -n "n;p" file3 2 4 6 8 10 [root@localhost wang]# sed '1!G;h;$!d' file3 10 9 8 7 6 5 4 3 2 1 [root@localhost wang]# sed '$!N;$!D' file3 9 10 [root@localhost wang]# sed '$!d' file3 10 [root@localhost wang]# sed 'G' file3 1 2 3 4 5 6 7 8 9 10 [root@localhost wang]# sed 'g' file3 [root@localhost wang]# sed '/^$/d;G' file3 1 2 3 4 5 6 7 8 9 10 [root@localhost wang]# sed 'n;d' file3 1 3 5 7 9 [root@localhost wang]# sed -n '1!G;h;$p' file3 10 9 8 7 6 5 4 3 2 1
原创文章,作者:DYW,如若转载,请注明出处:http://www.178linux.com/32769