文本处理之sed

文本编辑工具和 vi 不同,sed是行编辑器

sed工作原理
sed是从文件或管道中读取一行,处理一行,输出一行;再读取一行,再处理一行,再输出一行,直到最后一行。每当处理一行时,把当前处理的行存储在临时缓冲区中,称为模式空间(PatternSpace),
接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。
一次处理一行的设计模式使得sed性能很高,sed在读取大文件时不会出现卡顿的现象。
如果使用vi命令打开几十M上百M的文件,明显会出现有卡顿的现象,这是因为vi命令打开文件是一次性将文件加载到内存,然后再打开。
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 忽略大小写

范例:

[07:34:28 root@sz-centos7 /home]# sed  'p' /etc/issue
\S
\S
Kernel \r on an \m
Kernel \r on an \m

[08:20:05 root@sz-centos7 /home]# sed -n 'p' /etc/issue
\S
Kernel \r on an \m

[08:20:34 root@sz-centos7 /home]# sed -n '1p' /etc/passwd
root:x:0:0:root:/root:/bin/bash

[08:21:03 root@sz-centos7 /home]# ifconfig eth0 | sed -n '2p'
        inet 172.31.0.17  netmask 255.255.0.0  broadcast 172.31.255.255

[08:24:31 root@sz-centos7 /home]# ifconfig eth0 | sed -n '/netmask/p'
        inet 172.31.0.17  netmask 255.255.0.0  broadcast 172.31.255.255

范例:取最后一行

[08:22:08 root@sz-centos7 /home]# sed -n '$p' /etc/passwd
longxuan:x:8004:8004::/home/longxuan:/bin/bash

范例:

[08:25:42 root@sz-centos7 /home]# df | sed -n '/^\/dev\/sd/p'

范例:

[08:27:02 root@sz-centos7 /home]# seq 10 
1
2
3
4
5
6
7
8
9
10

[08:27:05 root@sz-centos7 /home]# seq 10 | sed -n '3,8p'
3
4
5
6
7
8

[08:27:37 root@sz-centos7 /home]# seq 10 | sed -n '3,+6p'
3
4
5
6
7
8
9

[08:27:43 root@sz-centos7 /home]# seq 10 | sed -n '3,$p'
3
4
5
6
7
8
9
10

[08:29:07 root@sz-centos7 /home]# seq 10 | sed -n '1~2p'
1
3
5
7
9

[08:29:31 root@sz-centos7 /home]# seq 10 | sed -n '2~2p'
2
4
6
8
10

[08:30:01 root@sz-centos7 /home]# seq 10 | sed  '2~2d'
1
3
5
7
9

[08:30:15 root@sz-centos7 /home]# seq 10 | sed  '1~2d'
2
4
6
8
10

范例:不显示注释行和空行

[08:32:28 root@sz-centos7 /home]# sed '/^#/d;/^$/d' /etc/httpd/conf/httpd.conf

[08:34:59 root@sz-centos7 /home]# grep -Ev '^#|^$' /etc/httpd/conf/httpd.conf

范例:

[08:34:59 root@sz-centos7 /home]# sed -i '/^listen 9527/a listen 80 \nlisten 8080'
/etc/httpd/conf/httpd.conf

范例:删除所有以#开头的行

[08:39:39 root@sz-centos7 /home]# sed -i '/^#/d' fstab

范例:只显示非#开头的行

[08:40:20 root@sz-centos7 /home]# sed -n '/^#/!p' fstab

范例:修改网卡配置

[08:41:35 root@sz-centos7 /home]# sed -Ei.bak '/^GRUB_CMDLINE_LINUX/s/(.*)(")$/\1 net.ifnames=0\2/' /etc/default/grub

范例: 搜索替换和&

[08:42:31 root@sz-centos7 /home]# sed -nr 's/r..t/&er/gp' /etc/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

范例: 获取分区利用率

[08:43:43 root@sz-centos7 /home]# df | sed -En '/^\/dev\/sd/s@.* ([0-9]+)%.*@\1@p'

范例:

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 地址

[08:57:13 root@sz-centos7 /home]# ifconfig eth0 | sed -nr "2s/[^0-9]+([0-9.]+).*/\1/p"
172.31.0.17

[09:00:35 root@sz-centos7 /home]# ifconfig eth0 | sed -En '2s/^[^0-9]+([0-9.]{7,15}).*/\1/p'
172.31.0.17

[09:01:00 root@sz-centos7 /home]# ifconfig eth0 | sed -nr '2s/^[^0-9]+([0-9.]+).*$/\1/p'
172.31.0.17

[09:02:07 root@sz-centos7 /home]# ifconfig eth0 | sed -n '2s/^.*inet //p' | sed -n 's/netmask.*//p'
172.31.0.17

[09:04:27 root@sz-centos7 /home]# ifconfig eth0 | sed -n '2s/^.*inet //;s/ netmask.*//p'
172.31.0.17

[09:05:54 root@sz-centos7 /home]# ifconfig eth0 | sed -nr '2s/(.*inet )([0-9].*)(netmask.*)/\2/p'
172.31.0.17

范例:取基名和目录名
取基名

[09:09:13 root@sz-centos7 /home]# echo "/etc/sysconfig/network-scripts/ifcfg-ens33" | sed -r 's@(^/.*/)([^/]+/?)@\2@'
ifcfg-ens33

取目录名

[09:10:13 root@sz-centos7 /home]# echo "/etc/sysconfig/network-scripts/ifcfg-ens33" | sed -r 's#(^/.*/)([^/]+)/?#\1#'
/etc/sysconfig/network-scripts/

范例: 取文件的前缀和后缀

[09:14:09 root@sz-centos7 /home]# echo a.b.c.gz | sed -En 's#(.*)\.([^.]+)$#\1#p'
a.b.c

[09:15:16 root@sz-centos7 /home]# echo a.b.c.gz | sed -En 's#(.*)\.([^.]+)$#\2#p'
gz

[09:15:34 root@sz-centos7 /home]# echo a.b.c.gz | grep -Eo '.*\.'
a.b.c.

[09:16:40 root@sz-centos7 /home]# echo a.b.c.gz | grep -Eo '[^.]+$'
gz

范例:

[09:16:55 root@sz-centos7 /home]# echo httpd-2.4.46.tar.bz2 | sed -nr 's@.*\.([^.]+\.)([^.]+)$@\1\2@p'
tar.bz2

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

[09:19:57 root@sz-centos7 /home]# sed -nr "s/^[^#]/#&/p" /etc/fstab 
#/dev/mapper/centos-root /                       xfs     defaults        0 0
#UUID=8fb7a3be-5e81-4f5d-ab35-afb996f29c9c /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

[09:21:11 root@sz-centos7 /home]# sed -nr 's/^[^#](.*)/#\1/p' /etc/fstab 
#dev/mapper/centos-root /                       xfs     defaults        0 0
#UID=8fb7a3be-5e81-4f5d-ab35-afb996f29c9c /boot                   xfs     defaults        0 0
#dev/mapper/centos-swap swap                    swap    defaults        0 0

[09:21:26 root@sz-centos7 /home]# sed -nr '/^#/!s@^@#@p' /etc/fstab 
#
#/dev/mapper/centos-root /                       xfs     defaults        0 0
#UUID=8fb7a3be-5e81-4f5d-ab35-afb996f29c9c /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

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

[09:22:55 root@sz-centos7 /home]# sed -ri.bak '/^#/s/^#//' /etc/fstab

范例:取分区利用率

[09:24:39 root@sz-centos7 /home]# df | sed -nr '/^\/dev\/sd/s# .* ([0-9]+)%.*# \1#p'
/dev/sda2 3
/dev/sda5 1
/dev/sda1 14

范例:修改内核参数

[09:25:24 root@sz-centos7 /home]# 

sed -nr '/^GRUB_CMDLINE_LINUX/s/"$/ net.ifnames=0"/p'
/etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=8363289d-138e-4e4a-abaf-
0454284fafggac rhgb quiet net.ifnames=0"

[09:25:24 root@sz-centos7 /home]# 
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-
0454284fafggac rhgb quiet net.ifnames=0"

[09:25:24 root@sz-centos7 /home]# 
sed -rn '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@p'
/etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=a0efb2bb-8227-4317-a79d-
0454284fafggac rhgb quiet net.ifnames=0 biosdevname=0"

范例:修改网卡名称

#centos7,8
[09:27:40 root@sz-centos7 /home]# sed -i '/GRUB_CMDLINE_LINUX=/s#quiet#& net.ifnames=0#'
/etc/default/grub

[09:27:40 root@sz-centos7 /home]# sed -ri '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@'
/etc/default/grub

[09:27:40 root@sz-centos7 /home]# grub2-mkconfig -o /boot/grub2/grub.cfg

#ubuntu
[09:27:40 root@BJ-Ubuntu /home]# grub-mkconfig -o /boot/grub/grub.cfg

范例:查看配置文件

#过滤掉空行和#开头的行
[09:29:46 root@sz-centos7 /home]# sed -r '/^(#|$)/d' /etc/httpd/conf/httpd.conf

[09:29:46 root@sz-centos7 /home]# sed -r '/^#|^$/d' /etc/httpd/conf/httpd.conf

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

[09:30:06 root@sz-centos7 /home]# sed -n -e '/^$/d' -e '/^[[:space:]]*#/!p' /etc/httpd/conf/httpd.conf

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

[09:30:24 root@sz-centos7 /home]# sed -n -e '/^[[:space:]]*#/!p' -e '/^$/d' /etc/httpd/conf/httpd.conf

范例:引用变量

[09:32:35 root@sz-centos7 /home]# echo | sed "s/^/$RANDOM.rmvb/"
5202.rmvb

[09:32:35 root@sz-centos7 /home]# echo | sed 's/^/$RANDOM.rmvb/'
$RANDOM.rmvb

[09:32:35 root@sz-centos7 /home]# echo | sed 's/^/'$RANDOM'.rmvb/'
13850.rmvb
[09:32:35 root@sz-centos7 /home]# echo | sed 's/^/'''$RANDOM'''.rmvb/'
28887.rmvb

范例:修改配置文件

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

范例: 变量实现多点编辑配置文件

[09:34:12 root@sz-centos7 /home]# port=8080
[09:34:12 root@sz-centos7 /home]# sed -ri.bak -e 's/^Listen 80/Listen '$port'/' -e "/ServerName/c
ServerName `hostname`:$port" /etc/httpd/conf/httpd.conf
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
seq 10 | sed -n '/3/{g;1!p;};h' #前一行
seq 10 | sed -nr '/3/{n;p}' #后一行
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

范例: 打印偶数行

[09:34:12 root@sz-centos7 /home]# seq 10 | sed -n 'n;p'
2
4
6
8
10

[09:38:32 root@sz-centos7 /home]# seq 10 | sed -n '2~2p'
2
4
6
8
10

[09:38:47 root@sz-centos7 /home]# seq 10 | sed  '1~2d'
2
4
6
8
10

[09:38:56 root@sz-centos7 /home]# seq 10 | sed -n '1~2!p'
2
4
6
8
10
posted @ 2021-04-08 21:41  空白的旋律  阅读(128)  评论(0编辑  收藏  举报