sed 插入多行_Linux三剑客之sed
sed命令用法小記
版本:CentOS7▼ ?▼??▼ ?▼??▼???▼??▼ ?▼??▼
好久沒(méi)更新文章了,項(xiàng)目的事情太多,總得給自己的懶惰找個(gè)借口,哈哈~
話不多說(shuō)進(jìn)入正題
創(chuàng)建測(cè)試數(shù)據(jù)
[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查看非注釋行和非空行的數(shù)據(jù)
[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從第一行開(kāi)始匹配到adm結(jié)束,打印之間的內(nèi)容
[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 ?在文件內(nèi)容末行添加一行
[alisca@spark02 a]$ sed '$ a\sed command end' myfilehello worldhello linuxhow are youi am finethanks, and you ?sed command end在匹配到的上一行插入一行數(shù)據(jù)
[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換行)數(shù)據(jù)
[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 ?在每行的開(kāi)頭添加Start,結(jié)尾添加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替換為T(mén)ail
[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克服懶惰的最好辦法就是現(xiàn)在行動(dòng)~希望今天的小知識(shí)能夠幫到你,歡迎轉(zhuǎn)發(fā)留言加關(guān)注哦,一起學(xué)習(xí),共同進(jìn)步~寫(xiě)在最后的話
生活不會(huì)向你許諾什么,尤其不會(huì)向你許諾成功。它只會(huì)給你掙扎、痛苦和煎熬的過(guò)程。所以要給自己一個(gè)夢(mèng)想,之后朝著那個(gè)方向前進(jìn)。如果沒(méi)有夢(mèng)想,生命也就毫無(wú)意義。——摩根·弗里曼
我今天才知道,我之所以漂泊就是在向你靠近。
--《廊橋遺夢(mèng)》
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的sed 插入多行_Linux三剑客之sed的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信新的朋友列表怎么恢复(想要加回误删的
- 下一篇: 优朋普乐大数据_优朋普乐邵以丁:用大数据