三剑客之sed流编辑器

三剑客之sed流编辑器

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

相关参数

  • -n suppress automatic printing of pattern space(即取消sed默认输出)

  • -e add the script to the commands to be executed(可支持连续使用sed)

  • -i edit files in place (makes backup if SUFFIX supplied)(支持源文件修改)

  • -r use extended regular expressions in the script.(支持扩展正则表达式)

内置参数

  • a Append text, which has each embedded newline preceded by a backslash

  • d Delete pattern space. Start next cycle.

  • i Insert text, which has each embedded newline preceded by a backslash.

  • p Print up to the first embedded newline of the current pattern space.

[root@gsh ~/test]# ifconfig eth0 >>test.txt
[root@gsh ~/test]# cat test.txt
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 1600 bytes 123578 (120.6 KiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 1953 bytes 2129196 (2.0 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-n
[root@gsh ~/test]# sed -n '2,3p' test.txt 
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
[root@gsh ~/test]# sed -n 2p test.txt
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
[root@gsh ~/test]# sed -n '/10.0.0.100/p' test.txt
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
-e
[root@gsh ~/test]# sed -n '/10.0.0.100/p' test.txt |sed -e 's#^.*inet ##g' -e 's#  netmask.*$##g'
10.0.0.100
-i
[root@gsh ~/test]# sed -i '/^$/d' test.txt 
[root@gsh ~/test]# cat test.txt
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 1600 bytes 123578 (120.6 KiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 1953 bytes 2129196 (2.0 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-r
[root@gsh ~/test]# sed -n 2p test.txt |sed -rn 's#^.*inet (.*)  netmask.*$#\1#gp'
10.0.0.100
[root@gsh ~/test]#
[root@gsh ~/test]# stat test.txt |sed -rn 's#^.*\(0(.*)/-rw.*$#\1#gp'
644
[root@gsh ~/test]#
a
[root@gsh ~/test]# sed '2a test' test.txt 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
test
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 1600 bytes 123578 (120.6 KiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 1953 bytes 2129196 (2.0 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@gsh ~/test]#
d
[root@gsh ~/test]# sed '/10.0.0.100/d' test.txt 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 1600 bytes 123578 (120.6 KiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 1953 bytes 2129196 (2.0 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@gsh ~/test]#
i
[root@gsh ~/test]# sed '2i test' test.txt 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
test
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 1600 bytes 123578 (120.6 KiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 1953 bytes 2129196 (2.0 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@gsh ~/test]#
p
[root@gsh ~/test]# sed -n 2p test.txt 
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
[root@gsh ~/test]# 

 

posted @ 2020-09-28 19:57  Gsh-123  阅读(94)  评论(0)    收藏  举报