sed命令用法小记
版本:CentOS7
▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
好久没更新文章了,项目的事情太多,总得给自己的懒惰找个借口,哈哈~
话不多说进入正题
创建测试数据
[alisca@spark02 a]$ cat data#test the sedThis is the header lineThis is the first data lineThis is the second data lineThis is the last line
查看非注释行和非空行的数据
[alisca@spark02 a]$ sed -n '/^#/!{/^$/!p}' data This is the header lineThis is the first data lineThis is the second data lineThis is the last line[alisca@spark02 a]$ sed -e '/^$/d' -e '/^#/d' dataThis is the header lineThis is the first data lineThis is the second data lineThis is the last line
从第一行开始匹配到adm结束,打印之间的内容
[alisca@spark02 a]$ sed -n '1,/adm/p' /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin
匹配插入,追加
[alisca@spark02 a]$ cat myfilehello worldhello linuxhow are youi am finethanks, and you ?
在匹配到文本的行首添加alisca
[alisca@spark02 a]$ sed -n '/world/s/^/alisca /p' myfilealisca hello world
在匹配到Linux前/后面添加alisca
[alisca@spark02 a]$ sed -n 's/linux/alisca &/p' myfilehello alisca linux[alisca@spark02 a]$ sed -n 's/linux/& alisca/p' myfilehello linux alisca
在匹配you的行尾添加alisca
[alisca@spark02 a]$ sed '/you/s/\(.*\)/\1 alisca/' myfilehello worldhello linuxhow are you aliscai am finethanks, and you ? alisca[alisca@spark02 a]$ sed '/you/s/$/ alisca/' myfilehello worldhello linuxhow are you aliscai am finethanks, and you ? alisca
在文件的首行添加一行
[alisca@spark02 a]$ sed '1 i\sed command start' myfile sed command starthello worldhello linuxhow are youi am finethanks, and you ?
在文件内容末行添加一行
[alisca@spark02 a]$ sed '$ a\sed command end' myfilehello worldhello linuxhow are youi am finethanks, and you ?sed command end
在匹配到的上一行插入一行数据
[alisca@spark02 a]$ sed '/are/i nihao' myfilehello worldhello linuxnihaohow are youi am finethanks, and you ?[alisca@spark02 a]$ sed '/are/i\nihao' myfile hello worldhello linuxnihaohow are youi am finethanks, and you ?
在匹配到的下一行添加一行或多行(\n换行)数据
[alisca@spark02 a]$ sed '/are/a nihao' myfilehello worldhello linuxhow are younihaoi am finethanks, and you ?[alisca@spark02 a]$ sed '/are/a\nihao\nI am alisca' myfilehello worldhello linuxhow are younihaoI am aliscai am finethanks, and you ?[alisca@spark02 a]$ sed "/are/a\nihao\nI'm alisca" myfile hello worldhello linuxhow are younihaoI'm aliscai am finethanks, and you ?
在每行的开头添加Start,结尾添加End
[alisca@spark02 a]$ sed 's/^/Start /' myfileStart hello worldStart hello linuxStart how are youStart i am fineStart thanks, and you ?[alisca@spark02 a]$ sed 's/$/ End/' myfile hello world Endhello linux Endhow are you Endi am fine Endthanks, and you ? End
在fine的前面添加very
[alisca@spark02 a]$ sed 's/fine/very &/' myfilehello worldhello linuxhow are youi am very finethanks, and you ?
在每行行尾添加End,将包含hello的行尾End替换为Tail
[alisca@spark02 a]$ sed -e 's/$/ End/' -e '/hello/s@End@Tail@' myfilehello world Tailhello linux Tailhow are you Endi am fine Endthanks, and you ? End
查找ip
[alisca@spark02 a]$ ifconfig ens33ens33: flags=4163 mtu 1500 inet 192.168.220.102 netmask 255.255.255.0 broadcast 192.168.220.255 inet6 fe80::20c:29ff:fe81:d000 prefixlen 64 scopeid 0x20 ether 00:0c:29:81:d0:00 txqueuelen 1000 (Ethernet) RX packets 2285146 bytes 1243543229 (1.1 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1787721 bytes 433034502 (412.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[alisca@spark02 a]$ ifconfig ens33|grep "\"|sed 's/.*inet //g'|sed 's/[:space:]*netmask.*//g'192.168.220.102
克服懒惰的最好办法就是现在行动~希望今天的小知识能够帮到你,欢迎转发留言加关注哦,一起学习,共同进步~写在最后的话
生活不会向你许诺什么,尤其不会向你许诺成功。它只会给你挣扎、痛苦和煎熬的过程。所以要给自己一个梦想,之后朝着那个方向前进。如果没有梦想,生命也就毫无意义。——摩根·弗里曼
我今天才知道,我之所以漂泊就是在向你靠近。
--《廊桥遗梦》