日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

sed 插入多行_Linux三剑客之sed

發(fā)布時間:2023/12/2 linux 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sed 插入多行_Linux三剑客之sed 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

sed命令用法小記

版本:CentOS7

▼ ?▼??▼ ?▼??▼???▼??▼ ?▼??▼

好久沒更新文章了,項目的事情太多,總得給自己的懶惰找個借口,哈哈~

話不多說進(jìn)入正題

創(chuàng)建測試數(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

從第一行開始匹配到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 ?

在每行的開頭添加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替換為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克服懶惰的最好辦法就是現(xiàn)在行動~希望今天的小知識能夠幫到你,歡迎轉(zhuǎn)發(fā)留言加關(guān)注哦,一起學(xué)習(xí),共同進(jìn)步~

寫在最后的話

生活不會向你許諾什么,尤其不會向你許諾成功。它只會給你掙扎、痛苦和煎熬的過程。所以要給自己一個夢想,之后朝著那個方向前進(jìn)。如果沒有夢想,生命也就毫無意義。——摩根·弗里曼

我今天才知道,我之所以漂泊就是在向你靠近。

--《廊橋遺夢》

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的sed 插入多行_Linux三剑客之sed的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。