sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
sed是一种流处理器,处理文本过程如下:
注意两点:
#命令行格式:将包含sed的命令写在命令行中执行 sed [options] ‘command‘ file(s) #脚本格式:将sed的命令写在一个脚本中,然后执行的时候,指定sed脚本的路径即可 sed [options] -f scriptfile file(s)
options可以使用下面几个值:
-e : 可以指定多个命令 command
-n: 与p(print)命令合用时,表示只显示被选中的行,而不是(显示所有的行,然后被选中的行会显示两次)。
-i:将sed的操作结果更新到文件中,因为默认的是不会操作文件本身的。
command:行定位(正则)+ sed命令,首先会通过正则行定位,选中要进行操作的行,然后执行sed命令
(base) [root@localhost Tana]# cat -n data.txt 1 log1.txt female BeiJing 90 Yes 2 log2.txt male ShangHai 55 3 log3.txt male chengdu 66 china 4 log4.txt female shengzhen 33 china 5 log5.txt male guangdong 22 America 6 log6.txt female hongkong 11 Japan (base) [root@localhost Tana]#
(base) [root@localhost Tana]# sed -n ‘6 p‘ data.txt log6.txt female hongkong 11 Japan
(base) [root@localhost Tana]# sed -n "/female/p" data.txt log1.txt female BeiJing 90 Yes log4.txt female shengzhen 33 china log6.txt female hongkong 11 Japan
(base) [root@localhost Tana]# sed -n "2,4p" data.txt log2.txt male ShangHai 55 log3.txt male chengdu 66 china log4.txt female shengzhen 33 china
(base) [root@localhost Tana]# cat -n data.txt | sed -n "/BeiJing/,/chengdu/p" 1 log1.txt female BeiJing 90 Yes 2 log2.txt male ShangHai 55 3 log3.txt male chengdu 66 china
在后面加 ! 即可
(base) [root@localhost Tana]# sed -n "4! p" data.txt log1.txt female BeiJing 90 Yes log2.txt male ShangHai 55 log3.txt male chengdu 66 china log5.txt male guangdong 22 America log6.txt female hongkong 11 Japan
(base) [root@localhost Tana]# sed -n "2,5! p" data.txt log1.txt female BeiJing 90 Yes log6.txt female hongkong 11 Japan
使用x~y格式,首先打印第x行,然后每个y行,就打印一次
(base) [root@localhost Tana]# sed -n "1~2p" data.txt log1.txt female BeiJing 90 Yes log3.txt male chengdu 66 china log5.txt male guangdong 22 America
sed有几个基本的操作命令,分别是下面几个:
1、a (append,添加,在行后追加)
2、i(insert,插入,在行前插入)
3、d(delete,删除行)
4、c(chage,替换)
5、s(substitute,替换)
(base) [root@localhost Tana]# sed "3a ******** " data.txt log1.txt female BeiJing 90 Yes log2.txt male ShangHai 55 log3.txt male chengdu 66 china ******** log4.txt female shengzhen 33 china log5.txt male guangdong 22 America log6.txt female hongkong 11 Japan
(base) [root@localhost Tana]# sed "3,5 a =========" data.txt log1.txt female BeiJing 90 Yes log2.txt male ShangHai 55 log3.txt male chengdu 66 china ========= log4.txt female shengzhen 33 china ========= log5.txt male guangdong 22 America ========= log6.txt female hongkong 11 Japan
i插入行和增加行的操作一样,区别是a是在行之后增加,i是在行之前插入
(base) [root@localhost Tana]# sed "3,5 i =====" data.txt log1.txt female BeiJing 90 Yes log2.txt male ShangHai 55 ===== log3.txt male chengdu 66 china ===== log4.txt female shengzhen 33 china ===== log5.txt male guangdong 22 America log6.txt female hongkong 11 Japan
替换行,是指,将指定行,整行内容都替换为指定内容,注意-s是指替换行中的一部分内容。注意,区间替换的时候,是整体替换,而不是逐行替换。
(base) [root@localhost Tana]# sed "2,5c hello" data.txt
log1.txt female BeiJing 90 Yes
hello
log6.txt female hongkong 11 Japan
(base) [root@localhost Tana]# sed "4,6d" data.txt log1.txt female BeiJing 90 Yes log2.txt male ShangHai 55 log3.txt male chengdu 66 china
这个和vim 的替换命令相同
(base) [root@localhost Tana]# sed "3 s/a/uu/g" data.txt log1.txt female BeiJing 90 Yes log2.txt male ShangHai 55 log3.txt muule chengdu 66 chinuu log4.txt female shengzhen 33 china log5.txt male guangdong 22 America log6.txt female hongkong 11 Japan
sed "/^$/ d"
2.获取eth0网卡的ip
(base) [root@localhost Tana]# ifconfig enp3s0f0 enp3s0f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.23.57.159 netmask 255.255.255.0 broadcast 172.23.57.255 inet6 fe80::225:90ff:fefc:34d2 prefixlen 64 scopeid 0x20<link> ether 00:25:90:fc:34:d2 txqueuelen 1000 (Ethernet) RX packets 468531 bytes 40207603 (38.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 15880 bytes 2790713 (2.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device memory 0xfb120000-fb13ffff (base) [root@localhost Tana]# ifconfig enp3s0f0 |sed -n "/inet/p " inet 172.23.57.159 netmask 255.255.255.0 broadcast 172.23.57.255 inet6 fe80::225:90ff:fefc:34d2 prefixlen 64 scopeid 0x20<link> (base) [root@localhost Tana]# ifconfig enp3s0f0 |sed -n "/inet.* netmask/p " inet 172.23.57.159 netmask 255.255.255.0 broadcast 172.23.57.255 (base) [root@localhost Tana]# ifconfig enp3s0f0 |sed -n "/inet.* netmask/p " |sed "s/inet//" 172.23.57.159 netmask 255.255.255.0 broadcast 172.23.57.255 (base) [root@localhost Tana]# ifconfig enp3s0f0 |sed -n "/inet.* netmask/p " |sed "s/inet//"|sed "s/net.*$//" 172.23.57.159
包括以下内容:
1、{command1; command2; command 3}多个sed命令,使用“;”分开
2、n表示跳1行
3、&表示前面已经匹配的字符串内容,反向引用,不用再写一次正则表达式
使用花括号{ }将多个sed命令包含在一起,多个sed之间用;分开 ,可以加也可以不加 { }
(base) [root@localhost Tana]# sed "3,5 d; s/male/Male/" data.txt log1.txt feMale BeiJing 90 Yes log2.txt Male ShangHai 55 log6.txt feMale hongkong 11 Japan
打印奇数行和偶数行
##打印奇数行
(base) [root@localhost Tana]# sed -n "1~2 p" data.txt log1.txt female BeiJing 90 Yes log3.txt male chengdu 66 china log5.txt male guangdong 22 America (base) [root@localhost Tana]# sed -n "{p;n}" data.txt log1.txt female BeiJing 90 Yes log3.txt male chengdu 66 china log5.txt male guangdong 22 America
##打印偶数行
(base) [root@localhost Tana]# sed -n "{n;p}" data.txt
log2.txt male ShangHai 55
log4.txt female shengzhen 33 china
log6.txt female hongkong 11 Japan
&表示前面已经匹配的字符串内容,反向引用,不用再写一次正则表达式
# 将 data.txt 里面的小写字母内容全部换成大写字母
(base) [root@localhost Tana]# sed "s/[a-z]/\u&/g" data.txt LOG1.TXT FEMALE BEIJING 90 YES LOG2.TXT MALE SHANGHAI 55 LOG3.TXT MALE CHENGDU 66 CHINA LOG4.TXT FEMALE SHENGZHEN 33 CHINA LOG5.TXT MALE GUANGDONG 22 AMERICA LOG6.TXT FEMALE HONGKONG 11 JAPAN
# 把大写字母全部换成小写字母
(base) [root@localhost Tana]# sed " s/[A-Z]/\l&/g" data.txt
log1.txt female beijing 90 yes
log2.txt male shanghai 55
log3.txt male chengdu 66 china
log4.txt female shengzhen 33 china
log5.txt male guangdong 22 america
log6.txt female hongkong 11 japan
# 将A.txt中的内容插入到B.txt中的第2行后面
sed ‘2 r A.txt‘ B.txt
#将A.txt中的内容插入到B.txt中包含CCCCCC的行后面
sed ‘/CCCCCC/ r A.txt‘ B.txt
#将A.txt中的内容插入B.txt中每一行的后面
sed ‘r A.txt‘ B.txt
原文:https://www.cnblogs.com/yan-2010/p/14298833.html