SRE运维工程师笔记-文本处理三剑客之 sed

内容概述

  • 文本处理三剑客之sed

1. 文本处理三剑客之 sed

官网:

http://sed.sourceforge.net/

1.1 sed 工作原理

sed 即 Stream EDitor,和 vi 不同,sed是行编辑器
在这里插入图片描述
Sed是从文件或管道中读取一行,处理一行,输出一行;再读取一行,再处理一行,再输出一行,直到最后一行。每当处理一行时,把当前处理的行存储在临时缓冲区中,称为模式空间(Pattern Space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。一次处理一行的设计模式使得sed性能很高,sed在读取大文件时不会出现卡顿的现象。如果使用vi命令打开几十M上百M的文件,明显会出现有卡顿的现象,这是因为vi命令打开文件是一次性将文件加载到内存,然后再打开。Sed就避免了这种情况,一行一行的处理,打开速度非常快,执行速度也很快

帮助参考网站:http://www.gnu.org/software/sed/manual/sed.html

1.2 sed 基本用法

格式:

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

常用选项:

-n                 不输出模式空间内容到屏幕,即不自动打印
-e                 多点编辑
-f    FILE       从指定文件中读取编辑脚本
-r, -E             使用扩展正则表达式
-i.bak            备份文件并原处编辑
-s                  将多个文件视为独立文件,而不是单个连续的长文件流

# 说明: 
-ir        不支持
-i -r      支持
-ri        支持
-ni       会清空文件

script格式:

'地址命令'

地址格式:

1. 不给地址:对全文进行处理
2. 单地址:
   #:指定的行,$:最后一行
   /pattern/:被此处模式所能够匹配到的每一行
3. 地址范围:
   #,#        #从#行到第#行,3,6 从第3行到第6行
   #,+#       #从#行到+#行,3,+4 表示从3行到第7行
   /pat1/,/pat2/
   #,/pat/
   /pat/,#
4. 步进:~
   1~2 奇数行
   2~2 偶数行

命令:

p              打印当前模式空间内容,追加到默认输出之后
Ip             忽略大小写输出
d              删除模式空间匹配的行,并立即启用下一轮循环
a [\]text      在指定行后面追加文本,支持使用\n实现多行追加
i [\]text      在行前面插入文本
c [\]text      替换行为单行或多行文本
w file         保存模式匹配的行至指定文件
r file         读取指定文件的文本至模式空间中匹配到的行后
=              为模式空间中的行打印行号
!              模式空间中匹配行取反处理
q              结束或退出sed

查找替代

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

范例:

[root@centos8 ~]#sed ''
welcome
welcome
to
to
magedu
magedu
[root@centos8 ~]#sed '' /etc/issue
\S
Kernel \r on an \m
[root@centos8 ~]#sed 'p' /etc/issue
\S
\S
Kernel \r on an \m
Kernel \r on an \m
[root@centos8 ~]#sed -n '' /etc/issue
[root@centos8 ~]#sed -n 'p' /etc/issue
\S
Kernel \r on an \m
[root@centos8 ~]#sed -n '1p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos8 ~]#ifconfig eth0 | sed '2p'
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::20c:29ff:fe45:a8a1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:45:a8:a1 txqueuelen 1000 (Ethernet)
RX packets 89815 bytes 69267453 (66.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 115634 bytes 79827662 (76.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos8 ~]#ifconfig eth0 | sed -n '2p'
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
[root@centos8 ~]#sed -n '$p' /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@centos8 ~]#ifconfig eth0 |sed -n '/netmask/p'
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
[root@centos8 ~]#df | sed -n '/^\/dev\/sd/p'
/dev/sda2 104806400 4872956 99933444 5% /
/dev/sda3 52403200 398860 52004340 1% /data
/dev/sda1 999320 848568 81940 92% /boot
[root@centos8 ~]#seq 10 | sed -n '3,6p'
3
4
5
6
[root@centos8 ~]#seq 10 | sed -n '3,+4p'
3
4
5
6
7
[root@centos8 ~]#seq 10 | sed -n '3,$p'
3
4
5
6
7
8
9
10
[root@centos8 ~]#seq 10 |sed -n '1~2p'
1 
3
5
7
9
[root@centos8 ~]#seq 10 |sed -n '2~2p'
2
4
6
8
10
[root@centos8 ~]#seq 10 |sed '1~2d'
2 
4
6
8
10
[root@centos8 ~]#seq 10 |sed '2~2d'
1
3
5
7
9
[root@centos8 ~]#sed -e '2d' -e '4d' seq.log
1
3
5
6
7
8
9
10
[root@centos8 ~]#sed '2d;4d' seq.log
1
3
5
6
7
8
9
10
[root@centos8 ~]#sed -i.orig '2d;4d' seq.log
[root@centos8 ~]#cat seq.log.orig
1 
2
3
4
5
6
7
8
9
10
[root@centos8 ~]#cat seq.log
1
3
5
6
7
8
9
10
[root@centos8 ~]#seq 10 > seq.log
[root@centos8 ~]#sed -i.orig '2d;4d' seq.log
[root@centos8 ~]#sed -i '/^listen 9527/a listen 80 \nlisten 8080'
/etc/httpd/conf/httpd.conf
#删除所有以#开头的行
[root@centos8 ~]#sed -i '/^#/d' fstab
#只显示非#开头的行
[root@centos8 ~]#sed -n '/^#/!p' fstab


#修改网卡配置
[root@centos8 ~]#sed -Ei.bak '/^GRUB_CMDLINE_LINUX/s/(.*)(")$/\1 net.ifnames=0\2/' /etc/default/grub

[root@CentOS-8 ~]#sed -ri.bak '/^GRUB_CMDLINE_LINUX/s#(.*)"$#\1 net.ifnames=0"#' /etc/default/grub
[root@CentOS-8 ~]#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="resume=UUID=faa14c8d-3932-41c2-a9c6-d80c41c54d4e rhgb quiet net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

[root@CentOS-8 ~]#sed -ri.bak '/^GRUB_CMDLINE_LINUX/s#"$# net.ifnames=0"#' /etc/default/grub
[root@CentOS-8 ~]#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="resume=UUID=faa14c8d-3932-41c2-a9c6-d80c41c54d4e rhgb quiet net.ifnames=0 net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

# Ubuntu的需要加个“=”
[root@CentOS-8 ~]#sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s#(.*)"$#\1 net.ifnames=0"#' /etc/default/grub
[root@CentOS-8 ~]#sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
#ubuntu 重新加载网卡驱动
grub-mkconfig -o /boot/grub/grub.cfg >& /dev/null
# centos 重新加载网卡驱动
grub2-mkconfig -o /boot/grub2/grub.cfg 

# 然后再重启
reboot

范例:多点编辑调用变量实现修改文件

[root@CentOS-8 ~]#name=root
[root@CentOS-8 ~]#sed -nr '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@CentOS-8 ~]#sed -nr '/$name/p' /etc/passwd
[root@CentOS-8 ~]#sed -nr "/$name/p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@CentOS-8 ~]#sed -nr '/'$name'/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@CentOS-8 ~]#vi /etc/httpd/conf/httpd.conf
[root@CentOS-8 ~]#port=8080
[root@CentOS-8 ~]#sed -ri.bak -e 's/^Listen 80/listen '$port'/' -e "/ServerName/c ServerName `hostname`:$port" /etc/httpd/conf/httpd.conf
[root@CentOS-8 ~]#vi /etc/httpd/conf/httpd.conf

范例:

sed '2p' /etc/passwd
sed -n '2p' /etc/passwd
sed -n '1,4p' /etc/passwd
sed -n '/root/p' /etc/passwd
sed -n '2,/root/p' /etc/passwd 从2行开始
sed -n '/^$/=' file 显示空行行号
sed -n -e '/^$/p' -e '/^$/=' file
Sed'/root/a\superman' /etc/passwd行后
sed '/root/i\superman' /etc/passwd 行前
sed '/root/c\superman' /etc/passwd 代替行
sed '/^$/d' file
sed '1,10d' file
nl /etc/passwd | sed '2,5d'
nl /etc/passwd | sed '2a tea'
sed 's/test/mytest/g' example
sed -n 's/root/&superman/p' /etc/passwd 单词后
sed -n 's/root/superman&/p' /etc/passwd 单词前
sed -e 's/dog/cat/' -e 's/hi/lo/' pets
sed -i.bak 's/dog/cat/g' pets

范例:取IP地址

[root@centos8 ~]#ifconfig eth0 |sed -nr "2s/[^0-9]+([0-9.]+).*/\1/p"
10.0.0.8
[root@centos8 ~]#ifconfig eth0 | sed -rn '2s/^[^0-9]+([0-9.]+) .*$/\1/p'
10.0.0.8
[root@centos8 ~]#ifconfig eth0 | sed -n '2s/^.*inet //p' | sed -n 's/
netmask.*//p'
10.0.0.8
[root@centos8 ~]#ifconfig eth0 | sed -n '2s/^.*inet //;s/ netmask.*//p'
10.0.0.8
[root@centos8 ~]#ifconfig eth0 | sed -rn '2s/(.*inet )([0-9].*)(
netmask.*)/\2/p'
10.0.0.8

范例:取基名和目录名

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

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

[root@centos8 ~]#sed -rn "s/^[^#]/#&/p" /etc/fstab
#UUID=1b950ef9-7142-46bd-975c-c4ac1e0d47e8 / xfs
defaults 0 0
#UUID=667a4c81-8b4b-4a39-a111-b11cb6d09309 /boot ext4
defaults 1 2
#UUID=38d14714-c018-41d5-922c-49e415decbca /data xfs
defaults 0 0
#UUID=a0efb2bb-8227-4317-a79d-0a70d515046c swap swap
defaults 0 0
[root@centos8 ~]#sed -rn 's/^[^#](.*)/#\1/p' /etc/fstab
#UID=1b950ef9-7142-46bd-975c-c4ac1e0d47e8 / xfs
defaults 0 0
#UID=667a4c81-8b4b-4a39-a111-b11cb6d09309 /boot ext4
defaults 1 2
#UID=38d14714-c018-41d5-922c-49e415decbca /data xfs
defaults 0 0
#UID=a0efb2bb-8227-4317-a79d-0a70d515046c swap swap
defaults 0 0
[root@centos8 ~]#sed -rn '/^#/!s@^@#@p' /etc/fstab
#
#UUID=1b950ef9-7142-46bd-975c-c4ac1e0d47e8 / xfs
defaults 0 0
#UUID=667a4c81-8b4b-4a39-a111-b11cb6d09309 /boot ext4
defaults 1 2
#UUID=38d14714-c018-41d5-922c-49e415decbca /data xfs
defaults 0 0
#UUID=a0efb2bb-8227-4317-a79d-0a70d515046c swap swap
defaults 0 0

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

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

范例:取分区利用率

[root@centos8 ~]#df | sed -nr '/^\/dev\/sd/s# .* ([0-9]+)%.*# \1#p'
/dev/sda2 3
/dev/sda5 1
/dev/sda1 14

范例:修改内核参数

[root@centos8 ~]#sed -nr '/^GRUB_CMDLINE_LINUX/s/"$/ net.ifnames=0"/p'
/etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=8363289d-138e-4e4a-abaf-
6e028babc924 rhgb quiet net.ifnames=0"
[root@centos8 ~]#sed -rn '/^GRUB_CMDLINE_LINUX=/s@(.*)"$@\1 net.ifnames=0"@p'
/etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=a0efb2bb-8227-4317-a79d-
0a70d515046c rhgb quiet net.ifnames=0"
[root@centos8 ~]#sed -rn '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@p'
/etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=a0efb2bb-8227-4317-a79d-
0a70d515046c rhgb quiet net.ifnames=0 net.ifnames=0"

范例:修改网卡名称

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

范例:查看配置文件

sed -r '/^(#|$)/d' /etc/httpd/conf/httpd.conf
sed -r '/^#|^$/d' /etc/httpd/conf/httpd.conf

范例:引用变量

[root@centos8 ~]#echo|sed "s/^/$RANDOM.rmvb/"
5242.rmvb
[root@centos8 ~]#echo|sed 's/^/$RANDOM.rmvb/'
$RANDOM.rmvb
[root@centos8 ~]#echo|sed 's/^/'''$RANDOM'''.rmvb/'

范例:修改配置文件

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

1.3 sed 高级用法

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

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

范例:

sed -n 'n;p' FILE
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@CentOS-8 ~]#seq 10 |sed -n 'n;p'
2
4
6
8
10
[root@CentOS-8 ~]#seq 10 |sed -n '2~2p'
2
4
6
8
10
[root@CentOS-8 ~]#seq 10 |sed  '1~2d'
2
4
6
8
10
[root@CentOS-8 ~]#seq 10 |sed -n '1~2!p'
2
4
6
8
10
[root@CentOS-8 ~]#seq 10 | sed 'N;s/\n//'
12
34
56
78
910
[root@CentOS-8 ~]#seq 10 | sed 'N;s/\n/\t/'
1       2
3       4
5       6
7       8
9       10

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

练习:
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为奇数行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值