追加用法總結
- 1、a 在匹配行后面追加
- 2、i 在匹配行前面追加
- 3、r 將文件內容追加到匹配行后面
- 4、w 將匹配行寫入指定文件
在匹配行后面追加 a
passwd文件第10行后面追加"Add Line Behind"
sed -i '10aAdd Line Behind' passwd
passwd文件第10行到第20行,每一行后面都追加"Test Line Behind"
sed -i '10,20a Test Line Behind' passwd
passwd文件匹配到/bin/bash的行后面追加"Insert Line For /bin/bash Behind"
sed -i '/\/bin\/bash/a Insert Line For /bin/bash Behind' passwd
在匹配行前面追加 i
passwd文件匹配到以nginx開頭的行,在匹配行前面追加"Add Line Before"
sed -i '/^nginx/i Add Line Before' passwd
passwd文件每一行前面都追加"Insert Line Before Every Line"
sed -i 'a Insert Line Before Every Line' passwd
將文件內容追加到匹配行后面 r
將/etc/fstab文件的內容追加到passwd文件第20行后面
sed -i '20r /etc/fstab' passwd
將/etc/inittab文件內容追加到passwd文件匹配到/bin/bash行的后面
sed -i '/\/bin\/bash/r /etc/inittab' passwd
將/etc/vconsole.conf文件內容追加到passwd文件中特定行后面,匹配以ftp開頭的行,到第18行的所有行
sed -i '/^ftp/,18r /etc/vconsole.conf' passwd
將匹配行寫入指定文件 w
將passwd文件匹配到/bin/bash的行追加到/tmp/sed.txt文件中
sed -i '/\/bin\/bash/w /tmp/sed.txt' passwd
將passwd文件從第10行開始,到匹配到/sbin/nologin的所有行內容追加到/tmp/sed-1.txt
sed -i '10,/\/sbin\/nologin/w /tmp/sed-1.txt' passwd
混合區間匹配讀取內容追加容易出錯 在處理幾十萬上百萬的文件中,可以找出特定的行,輸出到一個文件中,然后再對這個文件進行處理