shelllinux

7: 三剑客之sed

2021-03-25  本文已影响0人  随便写写咯

vim, grep 和sed工作原理对比

vim处理文件是把文件整个都加载到内存中打开,势必会因为文件过大造成消耗内存
grep和sed都逐行处理. 将每一行逐行加载到内存, 适合批量处理大文件
grep,sed可以处理文件, 也可以处理标准输入

格式

sed [option]... 'script;script;...' inputfile...

Script格式

'地址+命令组成' #脚本要放在单引号或者双引号
单地址: '2p','3d', 指定行号和命令

模式地址: '/模式/p', '/模式/d'
模式可以是正则表达式, 表示匹配包含该模式的行, 对这些行进行后续的命令操作
模式也可以是字符, 表示搜索包含某个字符串的行, 然后做命令操作

地址格式:

1. 不给地址: 对全文进行处理
2. 单地址: 
#: 指定第#行, $:最后一行
/pattern/: 被此处模式(正则表达式)所能够匹配到的每一行
3. 地址范围
#,#        表示从第#行. 到第#行, 例如: 3,6 表示第三行到第六行
#,+#      表示从第#行, 到第#+#行, 例如: 3,+6表示第三行到第九行
/pat1/, /pat2/  
#表示从符合pat1的行开始到符合pat2的行结束. 
#如果pat1不存在, pat2存在, 那么不会有任何输出, 如果pat1存在, pat2不存在, 那么会从pat1的行开始打印, 一直到结尾
#由于sed是逐行处理, 不可逆的, 因此如果匹配了一次pat1和pat2, 那么还会继续往下文找看看有没有符合pat1到pat2的模式的行. 
#, /pat/
4. 步进
1~2 奇数行
2~2 偶数行

常用选项:

-n 不输出模式空间内容到屏幕. 即不自动打印模式空间中的行
-e 多点编辑, 或关系
-f FILE 从指定文件中读取编辑脚本
-r, -E 使用扩展正则表达式
-i.bak 备份文件并在源文件基础上进行编辑
-i 直接修改源文件

命令:

# 注意, 命令和搜索替换的修饰符不同, 命令是配合script的"地址+命令"使用的, 而修饰符是搜索替换时使用的
p           打印当前模式空间内容,追加到默认输出之后, 也就是打印到屏幕
Ip          忽略大小写输出
d           删除模式空间匹配的行,并立即启用下一轮循环
a [\\]text  在指定行后面追加文本,支持使用\n实现多行追加
i [\\]text  在行前面插入文本
c [\\]text  替换行为单行或多行文本
w /path/file    保存模式匹配的行至指定文件
r /path/file    读取指定文件的文本至模式空间中匹配到的行后
=           为模式空间中的行打印行号
!           模式空间中匹配行取反处理

s/pattern/string/修饰符        查找替换,支持使用其它分隔符,可以是其它形式:s@@@,s###
替换修饰符:
g   行内全局替换
p   显示替换成功的行
w   /PATH/FILE  将替换成功的行保存至文件中
I,i   忽略大小写

案例1:

脚本中不写明地址和命令, 和cat效果一样, 打印文件内容到标准输出

sed默认行为就是读入一行到模式空间, 就把这行打印出来, 即使没有指定地址和命令, 也会自动打印, 可以用-n选项, 关闭自动打印

[root@ubuntu:/opt]# cat fstab 
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
# /data was on /dev/sda4 during installation
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
# swap was on /dev/sda5 during installation
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0


[root@ubuntu:/opt]# sed '' fstab # 脚本一定要用单引号或者双引号括起来, 即使没有地址和命令也要括起来, 否则会报错
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
# /data was on /dev/sda4 during installation
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
# swap was on /dev/sda5 during installation
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

案例2:

脚本中不写明地址和命令, 也不跟文件, 那就是打印, 读入一行, 打印一行

[01:07:41 root@c8prac /data/scripts]#sed ''
an
an
an
an
cd
cd
cd
cd
wa
wa

sed默认行为: 读入一行数据, 就会默认把这行数据打印到标准输出, 即使没有写任何地址和命令, 也会默认把读入到地址空间的数据, 打印到标准输出, 如果想取消自动打印, 需要加-n选项

也就是说,如果不加-n, 那么打印到标准输出的就是全文内容.

比如sed跟具体文件, 那么就是把文件的每一行都读入到模式空间, 由于没有加-n那么文件每一行读入到模式空间后, 就会默认被打印出来, 具体默认打印什么, 就是看哪些数据被读入到了sed模式空间里

案例3: 利用-n取消自动打印

[root@ubuntu:/opt]# sed '' fstab -n
[root@ubuntu:/opt]# 

[root@ubuntu:/opt]# sed '' -n
aa
bb
cc
dd
^C
^C
[root@ubuntu:/opt]# 
[01:18:13 root@c8prac /data/scripts]#sed -n ''
a
b
c
d

案例4: 利用sed的p命令, p命令会打印模式空间的内容到标准输出

# -n 选项指明取消自动打印, 因此读入到模式空间的每一行数据不会打印到标准输出
[root@ubuntu:/opt]# sed -n '' fstab
[root@ubuntu:/opt]#

 #-n 选项指明了取消自动打印, 但是p命令又指明了打印当前模式空间内容, 追加到默认输出之后, 因此 加载到模式空间的每一行又会被打印到标准输出
[root@ubuntu:/opt]# sed -n 'p' fstab 
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
# /data was on /dev/sda4 during installation
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
# swap was on /dev/sda5 during installation
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

案例5: 指定单地址, 直接在'脚本'中写行号p即可, 比如,1p,2p,3p, 分别代表打印第一行, 第二行, 第三行

[root@ubuntu:/opt]# sed -n '1p' fstab 
# /etc/fstab: static file system information.
[root@ubuntu:/opt]# sed -n '2p' fstab 
#

案例6: 打印网卡带有ip地址那一行

[root@ubuntu:/opt]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.29  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fe16:cbfe  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:16:cb:fe  txqueuelen 1000  (Ethernet)
        RX packets 57468  bytes 45311462 (45.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32433  bytes 2102696 (2.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@ubuntu:/opt]# ifconfig eth0 | sed -n  '2p'
        inet 10.0.0.29  netmask 255.255.255.0  broadcast 10.0.0.255

#如果不加-n, 那么除了目标第二行, 还会把所有的文本内容在打印一遍, 读入一行打印一行, 第二行会打印两边, 默认打印一遍, p命令打印一遍
[root@ubuntu:/opt]# ifconfig eth0 | sed  '2p'
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.29  netmask 255.255.255.0  broadcast 10.0.0.255
        inet 10.0.0.29  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fe16:cbfe  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:16:cb:fe  txqueuelen 1000  (Ethernet)
        RX packets 57522  bytes 45315880 (45.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32489  bytes 2107788 (2.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

案例7: 打印最后一行, $, 等价于 tail -n 1

[root@ubuntu:/opt]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@ubuntu:/opt]# seq 10 | sed -n '$p'
10

案例8: 找到/etc/fstab文件中, 以UUID开头的行

(单地址: /pattern/: 被此处模式(正则表达式)所能够匹配到的每一行, sed默认使用基本正则, 也可使用扩展正则

[root@ubuntu:/opt]# sed -n '/^UUID/p' fstab  # 如果指定了地址, 那么就一定要要跟命令, 数字地址直接跟命令, 而模式地址, 需要放在/ 地址 /中
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

案例9: 取多行(范围)

sed -n 'M,Np' FILE

取三到五行

[root@ubuntu:/opt]# seq 10 | sed -n '3,5p'
3
4
5

从第三行开始往后取6行, 也就是打印第三行到第九行

[root@ubuntu:/opt]# seq 10 | sed -n '3,+6p'
3
4
5
6
7
8
9

案例10: 过滤 /etc/passwd文件中, 从以b开头的行, 到以l开头的行

[01:49:32 root@CentOS-8-4 ~]#sed -n '/^b/,/^l/p' /etc/passwd
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

sed基于正则表达式的多行过滤

从满足第一个模式的行开始打印, 一直向下打印, 直到找到满足第二个模式的第一行, 然后把这两行和中间部分都打印出来, 之后继续向下找满足第一个模式的行, 如果有, 会继续打印, 重复第一步的过程, 但是如果只有满足第一个模式的行, 没有满足第二个模式的行, 那么就会把满足第一个模型的行, 之后的行全部打印, 因为sed是不可逆的

[root@ubuntu:/opt]# sed -n '/^b/,/^l/p' /etc/passwd
bin:x:2:2:bin:/bin:/usr/sbin/nologin # b开头
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin # l开头

[root@ubuntu:/opt]# sed -n '/^s/,/^l/p' /etc/passwd
sys:x:3:3:sys:/dev:/usr/sbin/nologin # s开头的第一行
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin # l 开头的第一行
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologin # 之后还有s开头的就继续打印
systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nologin
syslog:x:102:106::/home/syslog:/usr/sbin/nologin
messagebus:x:103:107::/nonexistent:/usr/sbin/nologin
_apt:x:104:65534::/nonexistent:/usr/sbin/nologin
lxd:x:105:65534::/var/lib/lxd/:/bin/false # 又找到了一个l开头的, 接着打印出来
sshd:x:109:65534::/run/sshd:/usr/sbin/nologin # 又有一个s开头的, 就继续打印, 但是后续已经没有l开头的行了, 那么就把所有剩余的行都打印出来
pollinate:x:110:1::/var/cache/pollinate:/bin/false
david:x:1000:1000:david,,,:/home/david:/bin/bash
haproxy:x:111:115::/var/lib/haproxy:/usr/sbin/nologin

案例11: 搜索几点几分到几点几分的日志

因为时间一般不会有重复的, 所有可以利用sed 的 '/pat1/,/pat2/p', 来取某个时间段内的日志, 直接匹配以指定的日期时间点开头的行即可

日志文件, 找出Mar 24 15:15到Mar 24 21:29之间的日志

[root@ubuntu:/opt]# tail -n 50 /var/log/syslog 
Mar 24 15:15:40 HAproxy-2 rsyslogd: imuxsock: Acquired UNIX socket '/run/systemd/journal/syslog' (fd 3) from systemd.  [v8.32.0]
Mar 24 15:15:40 HAproxy-2 rsyslogd: rsyslogd's groupid changed to 106
Mar 24 15:15:40 HAproxy-2 rsyslogd: rsyslogd's userid changed to 102
Mar 24 15:15:40 HAproxy-2 rsyslogd:  [origin software="rsyslogd" swVersion="8.32.0" x-pid="3536" x-info="http://www.rsyslog.com"] start
Mar 24 15:17:01 HAproxy-2 CRON[3613]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 15:26:39 HAproxy-2 systemd[1]: Starting Cleanup of Temporary Directories...
Mar 24 15:26:39 HAproxy-2 systemd[1]: Started Cleanup of Temporary Directories.
Mar 24 16:17:01 HAproxy-2 CRON[3655]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 17:17:01 HAproxy-2 CRON[3669]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 18:17:01 HAproxy-2 CRON[3683]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 18:24:37 HAproxy-2 systemd[1]: Starting Daily apt download activities...
Mar 24 18:24:39 HAproxy-2 systemd[1]: Started Daily apt download activities.
Mar 24 19:17:01 HAproxy-2 CRON[3777]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 20:17:01 HAproxy-2 CRON[3789]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 21:17:01 HAproxy-2 CRON[3798]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 21:28:59 HAproxy-2 systemd-resolved[731]: System hostname changed to 'ubuntu'.
Mar 24 21:29:00 HAproxy-2 systemd[1]: Stopping User Manager for UID 0...
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Stopped target Default.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Stopped target Basic System.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Stopped target Timers.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Stopped target Paths.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Stopped target Sockets.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Closed GnuPG cryptographic agent and passphrase cache.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Closed GnuPG cryptographic agent and passphrase cache (restricted).
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Closed REST API socket for snapd user session agent.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Closed GnuPG network certificate management daemon.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Closed GnuPG cryptographic agent (ssh-agent emulation).
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Closed GnuPG cryptographic agent and passphrase cache (access for web browsers).
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Reached target Shutdown.
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Starting Exit the Session...
Mar 24 21:29:00 HAproxy-2 systemd[1547]: Received SIGRTMIN+24 from PID 3804 (kill).
Mar 24 21:29:00 HAproxy-2 systemd[1]: Stopped User Manager for UID 0.
Mar 24 21:29:00 HAproxy-2 systemd[1]: Removed slice User Slice of root.
Mar 24 21:29:00 HAproxy-2 systemd[1]: Created slice User Slice of root.
Mar 24 21:29:00 HAproxy-2 systemd[1]: Started Session 12 of user root.
Mar 24 21:29:00 HAproxy-2 systemd[1]: Starting User Manager for UID 0...
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Listening on GnuPG cryptographic agent and passphrase cache (access for web browsers).
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Listening on REST API socket for snapd user session agent.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Listening on GnuPG cryptographic agent and passphrase cache.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Listening on GnuPG cryptographic agent (ssh-agent emulation).
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Reached target Timers.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Listening on GnuPG cryptographic agent and passphrase cache (restricted).
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Reached target Paths.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Listening on GnuPG network certificate management daemon.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Reached target Sockets.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Reached target Basic System.
Mar 24 21:29:00 HAproxy-2 systemd[1]: Started User Manager for UID 0.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Reached target Default.
Mar 24 21:29:00 HAproxy-2 systemd[3807]: Startup finished in 135ms.
Mar 24 22:17:01 HAproxy-2 CRON[4015]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

[root@ubuntu:/opt]# sed -n '/^Mar 24 15:15/,/^Mar 24 21:29/p' /var/log/syslog 
Mar 24 15:15:37 Ubuntu-1804-2 systemd[1]: Reloading.
Mar 24 15:15:37 Ubuntu-1804-2 systemd[1]: message repeated 2 times: [ Reloading.]
Mar 24 15:15:37 Ubuntu-1804-2 systemd[1]: Starting HAProxy Load Balancer...
Mar 24 15:15:37 Ubuntu-1804-2 systemd-udevd[505]: Network interface NamePolicy= disabled on kernel command line, ignoring.
Mar 24 15:15:37 Ubuntu-1804-2 haproxy[3123]: [NOTICE] 082/151537 (3123) : New worker #1 (3127) forked
Mar 24 15:15:37 Ubuntu-1804-2 systemd[1]: Started HAProxy Load Balancer.
Mar 24 15:15:38 Ubuntu-1804-2 systemd[1]: Reloading.
Mar 24 15:15:39 Ubuntu-1804-2 systemd[1]: Reloading.
Mar 24 15:15:40 HAproxy-2 rsyslogd:  [origin software="rsyslogd" swVersion="8.32.0" x-pid="831" x-info="http://www.rsyslog.com"] exiting on signal 15.
Mar 24 15:15:40 HAproxy-2 systemd[1]: Stopping System Logging Service...
Mar 24 15:15:40 HAproxy-2 systemd[1]: Stopped System Logging Service.
Mar 24 15:15:40 HAproxy-2 systemd[1]: Starting System Logging Service...
Mar 24 15:15:40 HAproxy-2 systemd-udevd[505]: Network interface NamePolicy= disabled on kernel command line, ignoring.
Mar 24 15:15:40 HAproxy-2 systemd[1]: Started System Logging Service.
Mar 24 15:15:40 HAproxy-2 rsyslogd: imuxsock: Acquired UNIX socket '/run/systemd/journal/syslog' (fd 3) from systemd.  [v8.32.0]
Mar 24 15:15:40 HAproxy-2 rsyslogd: rsyslogd's groupid changed to 106
Mar 24 15:15:40 HAproxy-2 rsyslogd: rsyslogd's userid changed to 102
Mar 24 15:15:40 HAproxy-2 rsyslogd:  [origin software="rsyslogd" swVersion="8.32.0" x-pid="3536" x-info="http://www.rsyslog.com"] start
Mar 24 15:17:01 HAproxy-2 CRON[3613]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 15:26:39 HAproxy-2 systemd[1]: Starting Cleanup of Temporary Directories...
Mar 24 15:26:39 HAproxy-2 systemd[1]: Started Cleanup of Temporary Directories.
Mar 24 16:17:01 HAproxy-2 CRON[3655]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 17:17:01 HAproxy-2 CRON[3669]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 18:17:01 HAproxy-2 CRON[3683]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 18:24:37 HAproxy-2 systemd[1]: Starting Daily apt download activities...
Mar 24 18:24:39 HAproxy-2 systemd[1]: Started Daily apt download activities.
Mar 24 19:17:01 HAproxy-2 CRON[3777]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 20:17:01 HAproxy-2 CRON[3789]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 21:17:01 HAproxy-2 CRON[3798]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Mar 24 21:28:59 HAproxy-2 systemd-resolved[731]: System hostname changed to 'ubuntu'.
Mar 24 21:29:00 HAproxy-2 systemd[1]: Stopping User Manager for UID 0...

案例12: 单地址, 和pattern模式可以混用

打印第10行, 到以l开头的行

[root@ubuntu:/opt]# sed -n '10,/^l/p' /etc/passwd
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin # 此时会打印pattern模式匹配到的第一行, 不会继续向下匹配

案例13: 步进

打印奇数行, 1~2

[root@ubuntu:/opt]# seq 10 | sed -n '1~2p' # 只有基于pattern正则表达式模式的匹配才需要/ /
1
3
5
7
9


打印偶数行, 2~2

[root@ubuntu:/opt]# seq 10 | sed -n '2~2p'
2
4
6
8
10

案例14: 忽略大小写 Ip, Ip是固定格式, 不能写成pI

[root@ubuntu:/opt]# sed -n '/^UUID/Ip' fstab 
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

案例15: d命令, 删除模式空间中匹配的行, 立即进入下一轮循环, 也就是某一行读取到模式空间后, 如果匹配, 那么就把这行从模式空间删除, 继续读取下一行到模式空间, 并不会修改文件, 只是不打印匹配到的行

可以做到对内容取反的效果, 把匹配到的行删掉, 不打印

[root@ubuntu:/opt]# sed '/^UUID/d' /etc/fstab # 默认开启自动打印, 但是使用d命令, 那么UUID开头的行不会打印
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
# /boot was on /dev/sda1 during installation
# /data was on /dev/sda4 during installation
# swap was on /dev/sda5 during installation

如果d和-n选项一起使用, 那么不会有任何输出, 因为-n就是取消默认打印, d命令又把匹配到的行删了

[root@ubuntu:/opt]# sed -n '/^UUID/d' /etc/fstab
[root@ubuntu:/opt]# 

案例16: ! 取反命令, 结合p, !p打印没有匹配到的行, 效果类似于d命令. 但是d命令如果和-n一起用, 就没有输出了. !p和-n一起使用, 只是匹配到的不打印, 没匹配到的还是会正常打印

!不是对命令取反, 而是对匹配的内容取反

[root@ubuntu:/opt]# sed -n '/^UUID/!p' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
# /boot was on /dev/sda1 during installation
# /data was on /dev/sda4 during installation
# swap was on /dev/sda5 during installation


[root@ubuntu:/opt]# sed  '/^UUID/d' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
# /boot was on /dev/sda1 during installation
# /data was on /dev/sda4 during installation
# swap was on /dev/sda5 during installation

# !d 不符合模式的就删了, 实际就是保留匹配到的行
[root@ubuntu:/opt]# sed '/^UUID/!d' /etc/fstab
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0
[root@ubuntu:/opt]# sed '/^UUID/!d' /etc/fstab -n
[root@ubuntu:/opt]# 


其他命令:

a [\] test 在指定行后面下一行追加文本, 支持使用\n实现多行追加
i [\] test 在指定行前面上一行插入文本
c [\] test 替换行为单行或多行文本
# a,i,c不能和-n选项一起使用, 否则就只会打印追加或者替换的文本, 把读入到模式空间的都取消打印了

a命令:下一行追加

案例17: a追加

#5a和line之间的空格加不加无所谓
[root@ubuntu:/opt]# seq 10 | sed  '5a line'
1
2
3
4
5
line
6
7
8
9
10

[root@ubuntu:/opt]# seq 10 | sed  '5aline'
1
2
3
4
5
line
6
7
8
9
10

案例18: 特定行之间, 每一行都追加相同的字符串

[root@ubuntu:/opt]# seq 10 | sed   '/^4/,/^9/alinux is good' 
1
2
3
4
linux is good
5
linux is good
6
linux is good
7
linux is good
8
linux is good
9
linux is good
10

[root@ubuntu:/opt]# sed  '/^UUID/a newline' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
newline
# /boot was on /dev/sda1 during installation
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
newline
# /data was on /dev/sda4 during installation
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
newline
# swap was on /dev/sda5 during installation
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0
newline

i命令: 上一行插入

[root@ubuntu:/opt]# seq 10 | sed '3i2222' 
1
2
2222
3
4
5
6
7
8
9
10

c命令: 替换匹配到的行为指定行

[root@ubuntu:/opt]# seq 10 | sed '3c2222' 
1
2
2222
4
5
6
7
8
9
10


配合\n实现多行追加, 插入, 或者替换多行

[root@ubuntu:/opt]# seq 10 | sed '5a line1\nline2\nline3'
1
2
3
4
5
line1
line2
line3
6
7
8
9
10

配合\实现在追加的字符串面前插入信息

# 如果想在追加的文本前面缩进四个空格, 那么只输入空格是不起作用的
[root@ubuntu:/opt]# seq 10 | sed '5a    line'
1
2
3
4
5
line
6
7
8
9
10

[root@ubuntu:/opt]# seq 10 | sed '5a\    line' # 需要配合\, 表示行首的开始, 后面加需要插入的内容
1
2
3
4
5
    line
6
7
8
9
10


w命令, 把匹配到的行写入新的文件

#注意单引号的位置, 要包含新的文件目录, 否则会显示sed找不到文件
[root@ubuntu:/opt]# sed -n '/^UUID/w /tmp/uuid.txt' /etc/fstab
[root@ubuntu:/opt]# cat /tmp/uuid.txt 
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

[root@ubuntu:/opt]# sed  '/^UUID/w /tmp/uuid1.txt' /etc/fstab # 如果没有加-n取消自动打印, 那么模式空间还是会输出到屏幕, 匹配到的行会写到文件
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
# /data was on /dev/sda4 during installation
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
# swap was on /dev/sda5 during installation
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0
[root@ubuntu:/opt]# cat /tmp/uuid1.txt 
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

r命令: 把一个文件的所有内容, 追加到匹配的行的下一行

[root@ubuntu:/opt]# sed '/^UUID/r  /etc/issue' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
Ubuntu 18.04.4 LTS \n \l

# /boot was on /dev/sda1 during installation
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
Ubuntu 18.04.4 LTS \n \l

# /data was on /dev/sda4 during installation
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
Ubuntu 18.04.4 LTS \n \l

# swap was on /dev/sda5 during installation
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0
Ubuntu 18.04.4 LTS \n \l


=等号命令: 打印匹配到的行的行号, 在行的上一行显示

[root@ubuntu:/opt]# sed  '/^UUID/=' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
9
UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
11
UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
# /data was on /dev/sda4 during installation
13
UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
# swap was on /dev/sda5 during installation
15
UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0


sed搜索替换

s/pattern/string/修饰符     查找替换, 支持使用其他分隔符, 可以是其他形式: s@@@, s###

替换修饰符:
g 行内全局替换, 一行中匹配到的字符全部替换
p 显示替换成功的行, 这个p和命令中的p不一样, 这里显示替换成功的行, 而命令p是打印当前模式空间内容,追加到默认输出之后, 也就是打印到屏幕
w /PATH/FILE 将替换成功的行保存至文件中
I, i  忽略大小写
[root@ubuntu:/opt]# sed 's/UUID/uuid/' /etc/fstab # 将文本中某个字符串, 替换成另一个字符串
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with uuid= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
uuid=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
uuid=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
# /data was on /dev/sda4 during installation
uuid=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
# swap was on /dev/sda5 during installation
uuid=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

g修饰符: 替换一行中, 匹配到的所有字符

图片.png 图片.png

&符号: 代表pattern中匹配的字符

图片.png

搜索替换配合分组

图片.png 图片.png 图片.png
# -n 和 p 修饰符要一起使用, 显示替换成的行. 一般在修改文件前, 先用-n和p修饰符, 查看替换会影响的行
[root@ubuntu:/opt]# ifconfig eth0 | sed -n '2p' | sed -nr 's#(.*inet )(.*)(netmask.*)#\2#p'
10.0.0.29 
[root@ubuntu:/opt]# ifconfig eth0 | sed -n '2p' | sed -r 's#(.*inet )(.*)(netmask.*)#\2#p' # 只有p修饰符, 没有-n选项, 那么会把匹配到的行打印两编, 取消-n会打印模式空间, p修饰符再打一遍匹配到的行
10.0.0.29  
10.0.0.29 

grep和sed都有贪婪模式

案例: CentOS 6.7.8, Ubuntu 通用获取ip地址

ip a show eth0 | sed -nr '3s#(^ *inet )(.*)(/.*)#\2#p' # sed '3s///' 针对某一行进行搜索替换, sed '//s###' 针对符合模式的行做搜索替换
[root@ubuntu:/opt]# ip a show eth0 | sed -n '3p' | sed -r 's#(^ *inet )(.*)(/.*)#\2#'
10.0.0.29

sed编辑修改文件

-i: 在源文件基础上修改
-i.bak: 先将源文件最备份, 添加.bak后缀, 然后在原文件基础上修改

#把UUID开头的行, 加上#号
[00:47:00 root@c8prac ~]#sed -ri '/^UUID/s@^@#@p' fstab  #-r后再-i, 如果是-i和p修饰符不能一起用, 会把匹配到的行, 又打印一遍
[root@ubuntu:/opt]# cat fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda3 during installation
#UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
#UUID=b6da1157-9484-4ebf-960b-98fd7e260c8e /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
#UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
#UUID=7fdab47c-1956-4be7-acb8-64e6f127c0bc /boot           ext4    defaults        0       2
# /data was on /dev/sda4 during installation
#UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
#UUID=8c74a351-2398-4ee2-a738-a39db67e9fd4 /data           ext4    defaults        0       2
# swap was on /dev/sda5 during installation
#UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0
#UUID=7fee41dd-6e09-48e0-bc8e-87b6c19f5e6b none            swap    sw              0       0

Centos 7&8, Ubuntu修改网卡名称

[01:17:00 root@c8prac ~]#sed -ri 's#(^.*quiet)"$#\1 net.ifnames=0"#' grub 
[01:17:07 root@c8prac ~]#sed -ri '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' grub 
grub-mkconfig -o /boot/grub/grub.cfg; reboot
grub-mkconfig -o /boot/grub/grub.cfg; reboot

Centos 6 修改网卡名称

[01:13:43 root@c6node ~]#sed -nr 's#(^.*NAME=")(.*)"$#\1eth1"#p' 70-persistent-net.rules 
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:2f:61:51", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

搜索替换总结:

  1. -n 和 p修饰符 成对使用会只显示替换成功的行, 如果-n单独使用, 不加p修饰符, 那么是没有输出的
  2. -i 和 p修饰符不要一起用
  3. 想查看替换结果, 就用 -n配合p, 想修改文件就用-i, 如果是扩展正则, 则要用-r
  4. 如果不涉及搜索替换, 不用s
  5. 搜索替换时, 不加-n也不加p修饰符, 那么就是把原文都显示, 该替换的替换
  6. 修改文件前, 先用-n和p修饰符,查看修改的结果, 确定没问题, 再去用-r -i.bak做修改, 如果是基本正则则不用-r
上一篇下一篇

猜你喜欢

热点阅读