26.第五章 文本处理三剑客之 sed(二)

范例:

[root@rocky8 ~]# sed '2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
raymond:x:1000:1000::/home/raymond:/bin/bash
jack:x:1001:1001::/home/jack:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@rocky8 ~]# sed -n '2p' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin

[root@rocky8 ~]# sed -n '1,4p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

[root@rocky8 ~]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@rocky8 ~]# sed -n '2,/root/p' /etc/passwd #从2行开始到有root的行
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@rocky8 ~]# sed -n '/^$/=' /etc/fstab  #显示空行行号
1
2
5
8
11

[root@rocky8 ~]# sed -n -e '/^$/p' -e '/^$/=' /etc/fstab  #显示空行和行号

1

2

5

8

11

[root@rocky8 ~]# sed '/root/a\superman' /etc/passwd #a行后插入
root:x:0:0:root:/root:/bin/bash
superman
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
superman
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
raymond:x:1000:1000::/home/raymond:/bin/bash
jack:x:1001:1001::/home/jack:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@rocky8 ~]# sed '/root/i\superman' /etc/passwd #i行前插入
superman
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
superman
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
raymond:x:1000:1000::/home/raymond:/bin/bash
jack:x:1001:1001::/home/jack:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@rocky8 ~]# sed '/root/c\superman' /etc/passwd #c代替行
superman
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
superman
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
raymond:x:1000:1000::/home/raymond:/bin/bash
jack:x:1001:1001::/home/jack:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@rocky8 ~]# sed '/^$/d' /etc/fstab 
 /etc/fstab
 Created by anaconda on Wed Oct  6 11:16:34 2021
 Accessible filesystems, by reference, are maintained under '/dev/disk/'.
 See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
 After editing this file, run 'systemctl daemon-reload' to update systemd
 units generated from this file.
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /                       xfs     defaults        0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot                   xfs     defaults        0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data                   xfs     defaults        0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none                    swap    defaults        0 0

[root@rocky8 ~]# sed '1,10d' /etc/fstab 

UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /                       xfs     defaults        0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot                   xfs     defaults        0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data                   xfs     defaults        0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none                    swap    defaults        0 0

[root@rocky8 ~]# nl /etc/passwd | sed '2,5d'
     1	root:x:0:0:root:/root:/bin/bash
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
    14	dbus:x:81:81:System message bus:/:/sbin/nologin
    15	systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
    16	systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
    17	tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
    18	polkitd:x:998:996:User for polkitd:/:/sbin/nologin
    19	unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    20	sssd:x:996:993:User for sssd:/:/sbin/nologin
    21	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    22	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    23	raymond:x:1000:1000::/home/raymond:/bin/bash
    24	jack:x:1001:1001::/home/jack:/bin/bash
    25	apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@rocky8 ~]# nl /etc/passwd | sed '2a tea'
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
tea
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
    14	dbus:x:81:81:System message bus:/:/sbin/nologin
    15	systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
    16	systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
    17	tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
    18	polkitd:x:998:996:User for polkitd:/:/sbin/nologin
    19	unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    20	sssd:x:996:993:User for sssd:/:/sbin/nologin
    21	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    22	postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    23	raymond:x:1000:1000::/home/raymond:/bin/bash
    24	jack:x:1001:1001::/home/jack:/bin/bash
    25	apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@rocky8 ~]# sed 's/root/myroot/g' /etc/passwd
myroot:x:0:0:myroot:/myroot:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/myroot:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
raymond:x:1000:1000::/home/raymond:/bin/bash
jack:x:1001:1001::/home/jack:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

[root@rocky8 ~]# sed -n 's/root/&superman/p' /etc/passwd #&代替前面的字符,单词后
rootsuperman:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/rootsuperman:/sbin/nologin

[root@rocky8 ~]# sed -n 's/root/superman&/p' /etc/passwd #单词前
supermanroot:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/supermanroot:/sbin/nologin

[root@rocky8 ~]# sed -e 's/dog/cat/' -e 's/hi/lo/' pets

[root@rocky8 ~]# sed -i.bak 's/dog/cat/g' pets

范例:面试题:找到20/May/2018:09:37:18到20/May/2018:09:40:18的日志

[root@rocky8 ~]# sed -n '/20\/May\/2018:09:37:18/,/20\/May\/2018:09:40:18/p' access_log 
172.16.102.29 - - [20/May/2018:09:37:18 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:18 +0800] "POST /webnoauth/model.cgi HTTP/1.1" 404 293 "http://172.16.0.1/webnoauth/model.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:18 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:18 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:18 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:18 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:21 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:21 +0800] "POST /webnoauth/model.cgi HTTP/1.1" 404 293 "http://172.16.0.1/webnoauth/model.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:21 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:21 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:21 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:21 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:24 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:24 +0800] "POST /webnoauth/model.cgi HTTP/1.1" 404 293 "http://172.16.0.1/webnoauth/model.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:24 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:24 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:24 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:37:24 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.16.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.16.102.29 - - [20/May/2018:09:40:18 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"

范例: 搜索替换和&

[root@rocky8 ~]# cp /etc/passwd .
[root@rocky8 ~]# grep root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@rocky8 ~]# grep 'r..t' passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

#后向引用
[root@rocky8 ~]# sed -rn 's/(r..t)/\1er/gp' passwd
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin
ftp:x:14:50:FTP User:/var/fterp:/sbin/nologin
[root@rocky8 ~]# sed -rn 's/(r..t)/pre\1/gp' passwd
preroot:x:0:0:preroot:/preroot:/bin/bash
operator:x:11:0:operator:/preroot:/sbin/nologin
ftp:x:14:50:FTP User:/vaprer/ftp:/sbin/nologin

#&
[root@rocky8 ~]# sed -n 's/r..t/&after/gp' passwd
rootafter:x:0:0:rootafter:/rootafter:/bin/bash
operator:x:11:0:operator:/rootafter:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftafterp:/sbin/nologin
[root@rocky8 ~]# sed -n 's/r..t/pre&/gp' passwd
preroot:x:0:0:preroot:/preroot:/bin/bash
operator:x:11:0:operator:/preroot:/sbin/nologin
ftp:x:14:50:FTP User:/vaprer/ftp:/sbin/nologin

范例:取IP 地址

[root@rocky8 ~]# ifconfig eth0 |sed -nr "2s/[^0-9]+([0-9.]+).*/\1/p"
172.31.1.8

[root@rocky8 ~]# ifconfig eth0 | sed -rn '2s/^[^0-9]+([0-9.]+) .*$/\1/p'
172.31.1.8

[root@rocky8 ~]# ifconfig eth0 | sed -n '2s/^.*inet //p' | sed -n 's/netmask.*//p'
172.31.1.8 

[root@rocky8 ~]# ifconfig eth0 | sed -n '2s/^.*inet //;s/ netmask.*//p'
172.31.1.8 

[root@rocky8 ~]# ifconfig eth0 | sed -rn '2s/(.*inet )([0-9].*)(netmask.*)/\2/p'
172.31.1.8 

[root@rocky8 ~]# ifconfig eth0 | sed -rn '2s@.*inet ([0-9.]+) .*@\1@p'
172.31.1.8

范例:取基名和目录名

[root@rocky8 ~]# echo "/etc/sysconfig/network-scripts/" |sed -r 's#(^/.*/)([^/]+/?)#\2#' #取基名
network-scripts/

[root@rocky8 ~]# echo "/etc/sysconfig/network-scripts/" |sed -r 's#(^/.*/)([^/]+/?)#\1#' 取目录
/etc/sysconfig/

#取目录名
[root@rocky8 ~]# echo /etc/sysconfig/ | sed -rn 's#(.*)/([^/]+)/?#\1#p'
/etc

#取基名
[root@rocky8 ~]# echo /etc/sysconfig/ | sed -rn 's#(.*)/([^/]+)/?#\2#p'
sysconfig

范例:将非#开头的行加#

[root@rocky8 ~]# sed -rn "s/^[^#]/#&/p" /etc/fstab
# /etc/fstab
# Created by anaconda on Wed Oct  6 11:16:34 2021
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /                       xfs     defaults        0 0
#UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot                   xfs     defaults        0 0
#UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data                   xfs     defaults        0 0
#UUID=4f387202-0a81-4202-9e12-90d04af4d308 none                    swap    defaults        0 0

[root@rocky8 ~]# sed -rn 's/^[^#](.*)/#\1/p' /etc/fstab
#/etc/fstab
#Created by anaconda on Wed Oct  6 11:16:34 2021
#Accessible filesystems, by reference, are maintained under '/dev/disk/'.
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#After editing this file, run 'systemctl daemon-reload' to update systemd
#units generated from this file.
#UID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /                       xfs     defaults        0 0
#UID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot                   xfs     defaults        0 0
#UID=02ae6744-8c28-4c86-b85b-e113c06a834f /data                   xfs     defaults        0 0
#UID=4f387202-0a81-4202-9e12-90d04af4d308 none                    swap    defaults        0 0

[root@rocky8 ~]#  sed -rn '/^#/!s@^@#@p' /etc/fstab
#
#
# /etc/fstab
# Created by anaconda on Wed Oct  6 11:16:34 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
#UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /                       xfs     defaults        0 0
#UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot                   xfs     defaults        0 0
#UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data                   xfs     defaults        0 0
#UUID=4f387202-0a81-4202-9e12-90d04af4d308 none                    swap    defaults        0 0

范例:将#开头的行删除#

[root@rocky8 ~]# sed -ri.bak '/^#/s/^#//' /etc/fstab

范例:取分区利用率

[root@rocky8 ~]# df |sed -nr '/^\/dev\/sd/s@^.* ([0-9]+)%.*@\1@p'
4
1
19
[root@rocky8 ~]# df |sed -nr '/^\/dev\/sd/s/^.* ([0-9]+)%.*/\1/p'
4
1
19
[root@rocky8 ~]# df |sed -nr '/^\/dev\/sd/s#^.* ([0-9]+)%.*#\1#p'
4
1
19

[root@rocky8 ~]# df | sed -nr '/^\/dev\/sd/s#.* ([0-9]+)%.*#\1#p'
4
1
19

范例:取系统版本

[root@rocky8 ~]# sed -rn '/^ID=/s@.*="?(\w+)"?@\1@p' /etc/os-release 
rocky
root@ubuntu1804:~# sed -rn '/^ID=/s@.*="?(\w+)"?@\1@p' /etc/os-release
ubuntu

[root@rocky8 ~]# sed -rn '/^ID=/s@.*="?([[:alpha:]]+)"?@\1@p' /etc/os-release 
rocky
root@ubuntu1804:~# sed -rn '/^ID=/s@.*="?([[:alpha:]]+)"?@\1@p' /etc/os-release
ubuntu

[root@rocky8 ~]# sed -rn '/^ID=/s@.*="?([a-z]+)"?@\1@p' /etc/os-release
rocky
root@ubuntu1804:~# sed -rn '/^ID=/s@.*="?([a-z]+)"?@\1@p' /etc/os-release
ubuntu

范例:修改内核参数

[root@rocky8 ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true
[root@rocky8 ~]#  sed -rn '/^GRUB_CMDLINE_LINUX/s#^(.*)(")$#\1 net.ifnames=0\2#p' /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet net.ifnames=0"
[root@rocky8 ~]# sed -ri.bak '/^GRUB_CMDLINE_LINUX/s#^(.*)(")$#\1 net.ifnames=0\2#' /etc/default/grub
#i 修改文件,加.bak就是修改前先备份文件

[root@rocky8 ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

[root@rocky8 ~]# \cp /etc/default/grub.bak /etc/default/grub

[root@rocky8 ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

[root@rocky8 ~]# sed -ri.bak '/^GRUB_CMDLINE_LINUX/s#"$# net.ifnames=0"#' /etc/default/grub
[root@rocky8 ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

[root@rocky8 ~]# \cp /etc/default/grub.bak /etc/default/grub
[root@rocky8 ~]# sed -nr '/^GRUB_CMDLINE_LINUX/s/"$/ net.ifnames=0"/p' /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet net.ifnames=0"

[root@rocky8 ~]# sed -rn '/^GRUB_CMDLINE_LINUX=/s@(.*)"$@\1 net.ifnames=0"@p' /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet net.ifnames=0"

[root@rocky8 ~]# sed -rn '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@p' /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=4f387202-0a81-4202-9e12-90d04af4d308 rhgb quiet net.ifnames=0"

范例:修改网卡名称

[root@rocky8 ~]# sed -ri '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@' /etc/default/grub
#centos7,8
[root@rocky8 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

#ubuntu
root@ubuntu1804:~# grub-mkconfig -o /boot/grub/grub.cfg

范例:查看配置文件

#过滤掉空行和#开头的行
[root@rocky8 ~]# sed -r '/^(#|$)/d' /etc/httpd/conf/httpd.conf
[root@rocky8 ~]# sed -r '/^#|^$/d' /etc/httpd/conf/httpd.conf

#可以排除行首后加多个空白符之后有#这种行
[root@rocky8 ~]# sed -n '/^$/d;/^[[:space:]]*#/!p' /etc/httpd/conf/httpd.conf
[root@rocky8 ~]# sed -n -e '/^$/d' -e '/^[[:space:]]*#/!p' /etc/httpd/conf/httpd.conf

#注意:以下前后顺序不同,执行效果不同
[root@rocky8 ~]# sed -n '/^[[:space:]]*#/!p;/^$/d' /etc/httpd/conf/httpd.conf
[root@rocky8 ~]# sed -n -e '/^[[:space:]]*#/!p' -e '/^$/d' /etc/httpd/conf/httpd.conf

范例:引用变量

[root@rocky8 ~]# echo |sed "s/^/$RANDOM.rmvb/"
27949.rmvb
[root@rocky8 ~]# echo |sed 's/^/$RANDOM.rmvb/'
$RANDOM.rmvb
#单引号不行
[root@rocky8 ~]# echo |sed 's/^/'''$RANDOM'''.rmvb/'
2103.rmvb
#在变量之间用三个单引号 引起来

[root@rocky8 ~]# echo |sed 's/^/''$RANDOM''.rmvb/'
$RANDOM.rmvb #两个单引号 不行
[root@rocky8 ~]# echo |sed 's/^/'$RANDOM'.rmvb/'
6170.rmvb #一个单引号可以
[root@rocky8 ~]# echo |sed 's/^/'''''$RANDOM'''''.rmvb/'
32204.rmvb #奇数 的都可以

范例:修改配置文件

[root@rocky8 ~]# sed -e '/^#<VirtualHost/,/^#<\/VirtualHost>/s@#@@' -e '/^#NameVirtualHost/s@#@@' /etc/httpd/conf/httpd.conf

[root@rocky8 ~]#  grep "^Listen" /etc/httpd/conf/httpd.conf
Listen  80

#把80端口改成8080
[root@rocky8 ~]# sed -ri.bak '/^Listen/s@(^Listen ).*@\1 8080@' /etc/httpd/conf/httpd.conf 
[root@rocky8 ~]# grep "^Listen" /etc/httpd/conf/httpd.conf
Listen  8080

范例:关闭SELinux

[root@rocky8 ~]# cat /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@rocky8 ~]# sed -ri '/^SELINUX=/s@(^SELINUX=).*@\1disabled@' /etc/sysconfig/selinux 
[root@rocky8 ~]# cat /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

5.3 sed 高级用法

sed 中除了模式空间,还另外还支持保持空间(Hold Space),利用此空间,可以将模式空间中的数据,临时保存至保持空间,从而后续接着处理,实现更为强大的功能。

常见的高级命令

P 打印模式空间开端至\n内容,并追加到默认输出之前
h 把模式空间中的内容覆盖至保持空间中
H 把模式空间中的内容追加至保持空间中
g 从保持空间取出数据覆盖至模式空间
G 从保持空间取出内容追加至模式空间
x 把模式空间中的内容与保持空间中的内容进行互换
n 读取匹配到的行的下一行覆盖至模式空间
N 读取匹配到的行的下一行追加至模式空间
d 删除模式空间中的行
D 如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间重新启动循环。如果模式空间不包含换行符,则会像发出d命令那样启动正常的新循环

范例:

sed -n 'n;p' FILE
seq 10 | sed 'N;s/\n//'
sed '1!G;h;$!d' FILE
sed ‘N;D’FILE
seq 10 |sed '3h;9G;9!d'
sed '$!N;$!D' FILE
sed '$!d' FILE
sed ‘G’ FILE
sed ‘g’ FILE
sed ‘/^$/d;G’ FILE
sed 'n;d' FILE
sed -n '1!G;h;$p' FILE

范例:

[root@rocky8 ~]# seq 10|sed -n 'n;p'
2
4
6
8
10

[root@rocky8 ~]# seq 10|sed 'N;s/\n//'
12
34
56
78
910

[root@rocky8 ~]# seq 10|sed 'N;s/\n/:/'
1:2
3:4
5:6
7:8
9:10

[root@rocky8 ~]# seq 10 | sed '1!G;h;$!d'
10
9
8
7
6
5
4
3
2
1

5.4 练习:

1、删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符
2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
3、在centos6系统/root/install.log每一行行首增加#号
4、在/etc/fstab文件中不以#开头的行的行首增加#号
5、处理/etc/fstab路径,使用sed命令取出其目录名和基名
6、利用sed 取出ifconfig命令中本机的IPv4地址
7、统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个字段的重复次数
8、统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)
9、将文本文件的n和n+1行合并为一行,n为奇数行

上一篇:shell数据筛选


下一篇:【sed】修改文件内容