Sed在匹配行前后加入一行


 

a 追加內容 sed ‘/匹配詞/a\要加入的內容’ example.file(將內容追加到匹配的目標行的下一行位置)
i 插入內容 sed ‘/匹配詞/i\要加入的內容’ example.file 將內容插入到匹配的行目標的上一行位置)
示例:
#我要把文件的包含“linux.com”這個關鍵詞的行前或行后加入一行,內容為“allow linux.cn”

1 #行前加
2 sed -i '/allow linux.com/i\allow linux.cn' the.conf.file
3 #行前后
4 sed -i '/allow linux.com/a\allow linux.cn' the.conf.file


---------------------------------------------------
1、刪除指定行的上一行
sed -i -e :a -e '$!N;s/.*\n\(.*ServerName abc.com\)/\1/;ta' -e 'P;D' $file
2、刪除指定字符串之間的內容
sed -i '/ServerName abc.com/,/\/VirtualHost/d' $file


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM