sed练习

[root@node2 scprits]# cat > data << eof
> This is the header line
> This is the first data line
> This is the secong data line
> This is the last line
> eof

打印文件第二行,第二行会打印两遍,sed默认会打印文件中的所有行。

[root@node2 scprits]# sed '2p' data
This is the header line
This is the first data line
This is the first data line
This is the secong data line
This is the last line
[root@node2 scprits]# sed -n '2p' data
This is the first data line

用-n选项则只打印匹配到的行。

[root@node2 scprits]# sed -n '1,3p' data
This is the header line
This is the first data line
This is the secong data line
打印1到3行
[root@node2 scprits]# sed -n '/secong/p' data
This is the secong data line
打印包含secong的所有行
[root@node2 scprits]# sed -n '/first/,4p' data
This is the first data line
This is the secong data line
This is the last line
打印匹配first的行到第4行
[root@node2 scprits]# sed -n '2,/last/p' data
This is the first data line
This is the secong data line
This is the last line
打印第2行到匹配到的last行
[root@node2 scprits]# sed -n '/header/,/last/p' data
This is the header line
This is the first data line
This is the secong data line
This is the last line
打印匹配到的header行到匹配到的last行的中间所有行
复制代码
[root@node2 scprits]# sed -n '1,4{=;p}' data
1
This is the header line
2
This is the first data line
3
This is the secong data line
4
This is the last line
打印文件中的第一行到第四行的内容,且打印行号,当用到sed不同的编辑命令时,用{},且不同的命令之间用分号隔开,
复制代码
[root@node2 scprits]# sed -n '1,2!{=;p}' data
3
This is the secong data line
4
This is the last line
用!表示对前面的匹配的模式取反
[root@node2 scprits]# sed -n '1,2!p' data
This is the secong data line
This is the last line
复制代码
[root@node2 scprits]# sed -n '1,5p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
[root@node2 scprits]# sed -n '/^ro*t/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@node2 scprits]# sed -n '/^r.*t/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@node2 scprits]# sed -n '/o\{1\}/p' /etc/passwd 
root:x:0:0:root:/root:/bin/bash
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
nginx:x:997:995::/home/nginx:/sbin/nologin
复制代码
[root@node2 scprits]# sed -n '/o\{2,3\}/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
复制代码
[root@node2 scprits]# sed -n '/^#/!p' /etc/vsftpd/vsftpd.conf 
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
复制代码
复制代码
[root@node2 scprits]# sed -n '/^#/!{/^$/!p}' /etc/vsftpd/vsftpd.conf 
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
复制代码

打印除#号开头和空行的所有的行。

复制代码
[root@node2 scprits]# sed -e '/^#/d' -e '/^$/d' /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
复制代码
[root@node2 scprits]# sed -n '1,/adm/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
[root@node2 scprits]# sed -i '1 i\sed command start' data
[root@node2 scprits]# cat data 
sed command start
This is the header line
This is the first data line
This is the secong data line
This is the last line
文件的行首添加一行
复制代码
[root@node2 scprits]#  sed -i '$a \sed command end' data
[root@node2 scprits]# cat data 
sed command start
This is the header line
This is the first data line
This is the secong data line
This is the last line
sed command end
文件的行尾追加一行
复制代码
复制代码
[root@node2 scprits]# cat myfile 
hello world
hello linux
how are you
i am fine
thanks,and you
[root@node2 scprits]# sed -n '/hello/s/^/Li /' myfile 
[root@node2 scprits]# sed  '/hello/s/^/Li /' myfile 
Li hello world
Li hello linux
how are you
i am fine
thanks,and you
在匹配helo字段的行,行首添加Li这个字符串。
复制代码
[root@node2 scprits]# sed 's/linux/jie &/' myfile 
hello world
hello jie linux
how are you
i am fine
thanks,and you
在匹配linux字符串的行,在linux字符串前加上jie
复制代码
[root@node2 scprits]# sed 's/linux/jie& /' myfile 
hello world
hello jielinux 
how are you
i am fine
thanks,and you

[root@node2 scprits]# sed 's/linux/& jie/' myfile 
hello world
hello linux jie
how are you
i am fine
thanks,and you
复制代码
[root@node2 scprits]# sed  '/you/s/$/Li/' myfile 
hello world
hello linux
how are youLi
i am fine
thanks,and youLi
在匹配you字符串的行的行尾加上Li
[root@node2 scprits]# sed '/you/s/\(.*\)/\1 Li/' myfile 
hello world
hello linux
how are you Li
i am fine
thanks,and you Li
用正则表达式,\(\)表示分组,.*表示任意字符,\1引用第一个分组,因为匹配的是任意字符,所整行都匹配了,在添加的时候就添加到行尾。
复制代码
[root@node2 scprits]# sed '/are/i nihao' myfile
hello world
hello linux
nihao
how are you
i am fine
thanks,and you
[root@node2 scprits]# sed '/are/i\nihao' myfile
hello world
hello linux
nihao
how are you
i am fine
thanks,and you
View Code
复制代码
[root@node2 scprits]# sed '/are/i/nihao' myfile
hello world
hello linux
/nihao
how are you
i am fine
thanks,and you
[root@node2 scprits]# sed '/are/a nihao' myfile
hello world
hello linux
how are you
nihao
i am fine
thanks,and you
复制代码
[root@node2 scprits]# sed '/are/a nihao\n wo hen hao' myfile
hello world
hello linux
how are you
nihao
 wo hen hao
i am fine
thanks,and you
追加多行要使用\n
复制代码
[root@node2 scprits]# sed 's/^/start /g' myfile 
start hello world
start hello linux
start how are you
start i am fine
start thanks,and you
start       每行开头加一个start
[root@node2 scprits]# sed 's/$/ end /g' myfile 
hello world end 
hello linux end 
how are you end 
i am fine end 
thanks,and you end 
 end 
[root@node2 scprits]# sed '1,3s/^/#/g' myfile 
#hello world
#hello linux
#how are you
i am fine
thanks,and you
1至3行打注释
复制代码

[root@node2 scprits]# sed 's/fine/very &/p' myfile
hello world
hello linux
how are you
i am very fine
i am very fine
thanks,and you

在匹配fine这行的前面加上very字符串,后面接p参数表上添加两行同样的,不加p参数只添加1行。

复制代码
[root@node2 scprits]# cat myfile 
sedcommand
#hello world tail and tail
#hello linux tail and tail
#how are you end
i am fine end
thanks,and you end
[root@node2 scprits]# sed -i -e '/sed/d' -e '/^$/d' myfile
[root@node2 scprits]# cat myfile
#hello world tail and tail
#hello linux tail and tail
#how are you end
i am fine end
thanks,and you end
[root@node2 scprits]# sed -i '/i/d' myfile
[root@node2 scprits]# sed -i '/end/d' myfile
[root@node2 scprits]# cat myfile

重点:sed命令实现对文件内容的替换(替换是在shell自动化脚本中用到最多的操作)

复制代码
[root@node2 scprits]# cat test.txt 
anonymous_enable=YES  
write_enable=YES  
local_umask=022  
xferlog_enable=YES  
connect_from_port_20=YES  
root:x:0:0:root:/root:/bin/bash  
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  
DEVICE="eth0"  
BOOTPROTO="static"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
IPADDR=172.16.22.1  
NETMASK=255.255.0.0  
复制代码
复制代码
[root@node2 scprits]# sed -i '/DEVICE/c\Ethernet' test.txt 
 #匹配DEVICE的行,替换成Ethernet这行 
[root@node2 scprits]# sed -i 's/static/dhcp/' test.txt
#把static替换成dhcp(/,@,#都是前面所说的地址定界符)   
[root@node2 scprits]# sed -i '/IPADDR/s@22\.1@10.12@' test.txt 
#匹配IPADDR的行,把22.1替换成10.12由于.号有特殊意义所有需要转义 
[root@node2 scprits]# sed -i '/connect/s#YES#NO#' test.txt 
#匹配connect的行,把YES替换成NO  
[root@node2 scprits]#  sed -i 's/bin/tom/2g' test.txt 
#把所有匹配到bin的行中第二次及第二次之后出现bin替换成tom  
[root@node2 scprits]# sed -i 's/daemon/jerry/2p' test.txt 
[root@node2 scprits]# sed -i 's/adm/boss/2' test.txt 
#把所有匹配到adm的行中仅仅只是第二次出现的adm替换成boss  
[root@node2 scprits]# sed -i '/root/{s/bash/nologin/;s/0/1/g}' test.txt 
#匹配root的行,把bash替换成nologin,且把0替换成1  
[root@node2 scprits]# sed -i 's/root/(&)/g' test.txt 
#把root用括号括起来,&表示引用前面匹配的字符 
[root@node2 scprits]# sed -i 's/BOOTPROTO/#BOOTPROTO/' test.txt 
#匹配BOOTPROTO替换成#BOOTPROTO,在配置文件中一般用于注释某行 
[root@node2 scprits]# sed -i 's/ONBOOT/#&/' test.txt 
匹配ONBOOT的行的前面添加#号,在配置文件中也表示注释某行 
[root@node2 scprits]# sed -i '/ONBOOT/s/#//' test.txt 
#匹配ONBOOT的行,把#替换成空,即去掉#号,也一般用作去掉#注释
复制代码
复制代码
[root@node2 scprits]# cat test.txt 
anonymous_enable=YES  
write_enable=YES  
local_umask=022  
xferlog_enable=YES  
connect_from_port_20=NO  
(root):x:1:1:(root):/(root):/bin/nologin  
bin:x:1:1:tom:/tom:/stom/nologin  
daemon:x:2:2:jerry:/sbin:/stom/nologin  
daemon:x:2:2:jerry:/sbin:/stom/nologin  
adm:x:3:4:boss:/var/adm:/sbin/nologin  
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin  
Ethernet
#BOOTPROTO="dhcp"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
IPADDR=172.16.10.12  
NETMASK=255.255.0.0  
复制代码

sed引用变量:(在自动化shell脚本 中也经常会使用到变量)

第一种当sed命令里面没有默认的变量时可以把单引号改成双引号;

第二种当sed命令里面有默认的变量时,那自己定义的变量需要加单引号,且sed里面的语句必须用单引

复制代码
[root@node2 ~]# cat myfile 
hello world  
i am jie  
how are you
[root@node2 ~]# name=li         #定义一个变量。
[root@node2 ~]# sed -i "s/jie/$name/" myfile
[root@node2 ~]# cat myfile
hello world  
i am li  
how are you
[root@node2 ~]# sed -i '$a '$name'' myfile  #在引用自定义的变量时,sed语句必须用单引引住,然后把自定义的变量也用单引号引住  
[root@node2 ~]# cat myfile 
hello world  
i am li  
how are you
li
复制代码

sed的其它高级使用:

1)把正在用sed操作的文件的内容写到例外一个文件中

复制代码
[root@node2 ~]# cat > test << eof
> Ethernet  
> #BOOTPROTO="dhcp"  
> HWADDR="00:0C:29:90:79:78"  
> ONBOOT="yes"  
> IPADDR=172.16.10.12  
> NETMASK=255.255.0.0 
> eof
[root@node2 ~]# sed -i 's/IPADDR/ip/w ip.txt' test  #把sed操作的文件内容保存到另外一个文件中,w表示保存,ip.txt文件名  
[root@node2 ~]# cat ip.txt
ip=172.16.10.12  
复制代码

读取一个文件到正在用sed操作的文件中

 

复制代码
[root@node2 ~]# cat myfile
hello world  
i am li  
how are you
li
[root@node2 ~]# cat test
Ethernet  
#BOOTPROTO="dhcp"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
ip=172.16.10.12  
NETMASK=255.255.0.0 
[root@node2 ~]# sed  -i '/Ethernet/r myfile' test      #在匹配Ethernet的行,读进来另一个文件的内容,读进来的文件的内容会插入到匹配Ethernet的行后 
[root@node2 ~]# cat test
Ethernet  
hello world  
i am li  
how are you
li
#BOOTPROTO="dhcp"  
HWADDR="00:0C:29:90:79:78"  
ONBOOT="yes"  
ip=172.16.10.12  
NETMASK=255.255.0.0 
复制代码

sed的经典例子:

复制代码
[root@node2 ~]# cat file 
http://www.baidu.com/index.<a target="_blank" href="http://www.2cto.com/kf/qianduan/css/" class="keylink" style="border:none; padding:0px; margin:0px; color:rgb(51,51,51); text-decoration:none; font-size:14px">html</a>  
http://www.baidu.com/1.html  
http://post.baidu.com/index.html  
http://mp3.baidu.com/index.html  
http://www.baidu.com/3.html  
http://post.baidu.com/2.html 
得到如下结果:  
域名的出现的次数 域名  
3 www.baidu.com  
2 post.baidu.com  
[root@node2 ~]# cat file  |awk -F "/" '{count[$3]++}END{for(i in count){print i,count[i]}}'
post.baidu.com 2
www.baidu.com 3
mp3.baidu.com 1
[root@localhost shell]# cat file | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort | uniq -c | sort -rn  
3 www.baidu.com  
2 post.baidu.com  
1 mp3.baidu.com  
root@node2 ~]# cat file | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort | uniq -c | sort -rn 
      3 www.baidu.com
      2 post.baidu.com
      1 mp3.baidu.com
复制代码

用grep结合sed取出网卡的ip地址 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @   星火撩原  阅读(516)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示