shell編程系列13--文本處理三劍客之sed利用sed追加文件內容

shell編程系列13--文本處理三劍客之sed利用sed追加文件內容

追加用法總結:
    1、a        在匹配行後面追加
    2、i        在匹配行前面追加
    3、r        將文件內容追加到匹配行後面
    4w        將匹配行寫入指定文件

追加用法示例詳解:
    1、a
        (1)、passwd文件第10行後面追加"Add Line Behind"
            sed -i '10aAdd Line Behind' passwd 
        (2)、passwd文件第10行到第20行,每一行後面都追加"Test Line Behind"
            sed -i '10,20a Test Line Behind' passwd
        (3)、passwd文件匹配到/bin/bash的行後面追加"Insert Line For /bin/bash Behind"
            sed -i '/\/bin\/bash/a Insert Line For /bin/bash Behind' passwd

    2、i
        (1)、passwd文件匹配到以nginx開頭的行,在匹配行前面追加"Add Line Before"
            sed -i '/^nginx/i Add Line Before' passwd
        (2)、passwd文件每一行前面都追加"Insert Line Before Every Line"
            sed -i 'a Insert Line Before Every Line' passwd

    3、r
        (1)、將/etc/fstab文件的內容追加到passwd文件第20行後面
            sed -i '20r /etc/fstab' passwd
        (2)、將/etc/inittab文件內容追加到passwd文件匹配到/bin/bash行的後面
            sed -i '/\/bin\/bash/r /etc/inittab' passwd
        (3)、將/etc/vconsole.conf文件內容追加到passwd文件中特定行後面,匹配以ftp開頭的行,到第18行的全部行
            sed -i '/^ftp/,18r /etc/vconsole.conf' passwd

    4w
        (1)、將passwd文件匹配到/bin/bash的行追加到/tmp/sed.txt文件中
            sed -i '/\/bin\/bash/w /tmp/sed.txt' passwd
        (2)、將passwd文件從第10行開始,到匹配到/sbin/nologin的全部行內容追加到/tmp/sed-1.txt
            sed -i '10,/\/sbin\/nologin/w /tmp/sed-1.txt' passwd

# 大寫字母替換爲小寫字母

混合區間匹配讀取內容追加容易出錯
在處理幾十萬上百萬的文件中,能夠找出特定的行,輸出到一個文件中,而後再對這個文件進行處理
相關文章
相關標籤/搜索