shell中sed的簡單使用


sed命令行格式為:
         sed [-nefri]  ‘command’  輸入文本/文件        
常用選項:
        -n∶取消默認的輸出,使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN的資料一般都會被列出到屏幕上。但如果加上 -n 參數后,則只有經過sed 特殊處理的那一行(或者動作)才會被列出來
        -e∶進行多項編輯,即對輸入行應用多條sed命令時使用. 直接在指令列模式上進行 sed 的動作編輯
        -f∶指定sed腳本的文件名. 直接將 sed 的動作寫在一個檔案內, -f filename 則可以執行 filename 內的sed 動作
        -r∶sed 的動作支援的是延伸型正則表達式的語法。(預設是基礎正則表達式語法)
        -i∶直接修改讀取的文件內容,而不是由屏幕輸出       
常用命令:
        a ∶ 新增, a 的后面可以接字串,而這些字串會在新的一行出現(目前的下一行)
        c ∶ 取代, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行
        d ∶ 刪除,因為是刪除,所以 d 后面通常不接任何內容
        i ∶ 插入, i 的后面可以接字串,而這些字串會在新的一行出現(目前的上一行)
        p∶ 列印,亦即將某個選擇的資料印出。通常 p 會與參數 sed -n 一起用
        s∶ 取代,可以直接進行替換的工作。通常這個 s 的動作可以搭配正則表達式。例如 1,20s/old/new/g


定址
定址用於決定對哪些行進行編輯。地址的形式可以是數字、正則表達式、或二者的結合。如果沒有指定地址,sed將處理輸入文件的所有行。
地址是一個數字,則表示行號;是“$"符號,則表示最后一行。例如:  
sed  -n  '3p' datafile
只打印第三行


指定一個區間行的信息
sed -n '2,4p' datafile

地址是逗號分隔的,那么需要處理的地址是這兩行之間的范圍(包括這兩行在內)。范圍可以用數字、正則表達式、或二者的組合表示。例如:
 
 
sed '2,5d' datafile
#刪除第二到第五行
sed '/My/,/You/d' datafile
#刪除包含"My"的行到包含"You"的行之間的行
sed '/My/,10d' datafile
#刪除包含"My"的行到第十行的內容
 

刪除某行
   [root@localhost ruby] # sed '1d' a.txt                 #刪除第一行
   [root@localhost ruby] # sed '$d' a.txt                #刪除最后一行
   [root@localhost ruby] # sed '1,2d' a.txt             #刪除第一行到第二行
   [root@localhost ruby] # sed '2,$d' a.txt             #刪除第二行到最后一行
顯示某行
   [python@master2 tmp]$ sed -n '1p' a.txt           #顯示第一行
   [python@master2 tmp]$ sed -n '$p' a.txt           #顯示最后一行
   [python@master2 tmp]$ sed -n '1,2p' a.txt        #顯示第一行到第二行
   [python@master2 tmp]$ sed -n '2,$p' a.txt        #顯示第二行到最后一行
使用模式進行查詢
   [python@master2 tmp]$ sed -n '/2/p'  a.txt        #查詢包括關鍵字ruby所在所有行
   [python@master2 tmp]$ sed -n '/\$/p' a.txt        #查詢包括關鍵字$所在所有行,使用反斜線\屏蔽特殊含義



增加一行或多行字符串
[python@master2 tmp]$ sed '1a drink tea' a.txt      #第一行后增加字符串
1
drink tea
2
3 the $
4
5
6
7
8 the
9

[python@master2 tmp]$ sed '1,3a drink tea' a.txt   #在第一行和第三行之間的行每一行之間都添加
1
drink tea
2
drink tea
3 the $
drink tea
4
5
6
7
8 the
9
[python@master2 tmp]$ sed '1a drink tea\nor coffen' a.txt  #添加多行用\n來鏈接
1
drink tea
or coffen
2
3 the $
4
5
6
7
8 the
9


代替一行或者多行
[python@master2 tmp]$ sed '1c Hi' a.txt  #第一行用Hi代替
Hi
2
3 the $
4
5
6
7
8 the
9
[python@master2 tmp]$ sed '1,2c Hi' a.txt  #第一行和第二行用Hi代替
Hi
3 the $
4
5
6
7
8 the
9


替換一行中的某部分
格式:sed 's/要替換的字符串/新的字符串/g'   (要替換的字符串可以用正則表達式)
[python@master2 tmp]$ sed -n '/the/p' a.txt |sed 's/the/bird/g'   #代替
3 bird $
8 bird
[python@master2 tmp]$ sed -n '/the/p' a.txt |sed 's/the//g'        #刪除
3  $
8


插入
[python@master2 tmp]$ sed -i '$abye' a.txt  #在最后一行添加bye



替換:
-e是編輯命令,用於sed執行多個編輯任務的情況下。在下一行開始編輯前,所有的編輯動作將應用到模式緩沖區中的行上。
[python@master2 tmp]$ sed -e '1,3d' -e 's/the/your/g'  a.txt     #刪除1到3行,然后the用your 代替
4
5
6
7
8 your
9
bye

# 替換兩個或多個空格為一個空格
sed 's/[ ][ ]*/ /g' file_name


# 替換兩個或多個空格為分隔符:
sed 's/[ ][ ]*/:/g' file_name
    

# 替換成空格
sed 's/[[:space:]][[:space:]]*/ /g' filename


# 替換成分隔符:
sed 's/[[:space:]][[:space:]]*/:/g' filename


基本sed編程舉例:
    使用p(rint)顯示行: sed -n '2p' temp.txt             只顯示第2行,使用選項n
    打印范圍:  sed -n '1,3p' temp.txt                   打印第1行到第3行
    打印模式:  sed -n '/movie/'p temp.txt               打印含movie的行
    使用模式和行號查詢:  sed -n '3,/movie/'p temp.txt   只在第3行查找movie並打印
    顯示整個文件:  sed -n '1,$'p temp.txt               $為最后一行
    任意字符:  sed -n '/.*ing/'p temp.txt               注意是.*ing,而不是*ing
    打印行號:  sed -e '/music/=' temp.txt
    刪除文本: sed '1d' temp.txt  或者 sed '1,4d' temp.txt
    替換文本: sed 's/source/OKSTR/' temp.txt            將source替換成OKSTR
             sed 's//$//g' temp.txt                     將文本中所有的$符號全部刪除
             sed 's/source/OKSTR/w temp2.txt' temp.txt  將替換后的記錄寫入文件temp2.txt
    替換修改字符串: sed 's/source/"ADD BEFORE" &/p' temp.txt
             結果將在source字符串前面加上"ADD BEFORE",這里的&表示找到的source字符並保存
    sed結果寫入到文件: sed '1,2 w temp2.txt' temp.txt
                       sed '/name/ w temp2.txt' temp.txt
    從文件中讀文本: sed '/name/r temp2.txt' temp.txt
    在每列最后加文本: sed 's/[0-9]*/& Pass/g' temp.txt
    從shell向sed傳值: echo $NAME | sed "s/go/$REP/g"   注意需要使用雙引號
快速一行命令:
    's//.$//g'         刪除以句點結尾行
    '-e /abcd/d'       刪除包含abcd的行
    's/[][][]*/[]/g'   刪除一個以上空格,用一個空格代替
    's/^[][]*//g'      刪除行首空格
    's//.[][]*/[]/g'   刪除句號后跟兩個或更多的空格,用一個空格代替
    '/^$/d'            刪除空行
    's/^.//g'          刪除第一個字符,區別  's//.//g'刪除所有的句點
    's/COL/(.../)//g'  刪除緊跟COL的后三個字母
    's/^////g'         刪除路徑中第一個/



替換單引號為空:
可以這樣寫:
sed 's/'"'"'//g'

sed 's/'\''//g'

sed s/\'//g



在文件的第一行前面插入一行abc
[python@master2 tmp]$ sed -i '1i\abc' a.txt


免責聲明!

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



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