码迷,mamicode.com
首页 > 其他好文 > 详细

文本处理三剑客之 sed

时间:2020-04-12 16:48:24      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:code   ref   再处理   保持空间   rtu   扩展正则   queue   实现   递增   

一级标题

文本处理三剑客之 sed

二级标题

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

二级标题

2 sed 基本用法
格式
sed [option]... ‘script;script;...‘ inputfile...

常用选项:
-n 不输出模式空间内容到屏幕,即不自动打印
-e 多点编辑
-f /PATH/SCRIPT_FILE 从指定文件中读取编辑脚本
-r, -E 使用扩展正则表达式
-i.bak 备份文件并原处编辑

script格式:
‘地址命令‘

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

命令:
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 ?忽略大小写

范例:
[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

范例
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

二级标题

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

二级标题

?本处理三剑客之SED面试题

1、linux系统中,__cut__命令可以从?本?件的每??中截取指定的内容的数据。

2、在每??后增加?空??
[root@magedu ~]# sed ‘G‘ text.txt

3、在匹配regex的?之后插??空??
[root@magedu ~]# sed ‘/regex/G‘ text.txt

4、计算?件?数?
cat 1.sh|wc -l


6、sed将?件test中第50?中的haiwao改为haiwai?
[root@magedu ~]# sed -n ‘50s/haiwao/haiwei/p‘ test


7、替换一个文件/etc/passwd里的这root:x:0:0:root:/root:/bin/bash一行第二个root为test?
cat /etc/passwd| sed ‘/^root/!d‘|sed ‘s/root/test/2‘


8、打印/etc/passwd的奇数??
[root@magedu ~]# sed -n ‘1~2p‘ /etc/passwd



9、?志?件a.log,内容是时间顺序递增,从0点到23点的所有?志记录,每条时间的?志为??:
2016/06/12 00:00:00 - - 200 190 http://www.a.com/o1html xxxxxxx
2016/06/12 00:00:01 - - 200 390 http://www.b.com/o1html xxxxxxx
2016/06/12 00:00:02 - - 200 490 http://www.v.com/o.html xxxxxxx
2016/06/12 00:00:03 - - 200 890 http://www.a.com/o.html xxxxxxx
......
2016/06/12 23:59:56 - - 200 320 http://www.3.com/o.jpg xxxxxxx
2016/06/12 23:59:57 - - 200 131 http://www.9.com/o.html xxxxxxx
2016/06/12 23:59:58 - - 200 489 http://www.r.com/o.net xxxxxxx
2016/06/12 23:59:59 - - 200 772 http://www.w.com/o.php xxxxxxx

打印出05点到12点之间的所有?志?打印出05:30:35到22:45:55之间的所有?志?
sed -n ‘/2016\/06\/12 05:00:00/,/2016\/06\/12 12:00:00/p‘ a.log
sed -n ‘/2016\/06\/12 05:30:35/,/2016\/06\/12 22:45:55/p‘ a.log

文本处理三剑客之 sed

标签:code   ref   再处理   保持空间   rtu   扩展正则   queue   实现   递增   

原文地址:https://www.cnblogs.com/zhaihongyu/p/12685785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!