【Linux】sed命令初接觸(1)

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

追加用法示例詳解:
    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

    4、w
        (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

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

混合區間匹配讀取內容追加容易出錯
在處理幾十萬上百萬的文件中,可以找出特定的行,輸出到一個文件中,然後再對這個文件進行處理
————————————————
版權聲明:本文爲CSDN博主「鄭子明」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/reblue520/article/details/91858258

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章