linux-sed

【sed基礎使用】
[root@localhost test]#man sed
SED(1)                           User Commands                          SED(1)
NAME
       sed - stream editor for filtering and transforming text
  【命令參數格式】
SYNOPSIS
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
       Sed  is  a stream editor.  A stream editor is used to perform basic text transformations on an input stream
       (a file or input from a pipeline).  While in some ways similar to an editor which  permits  scripted  edits
       (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But
       it is sed’s ability to filter text in a pipeline which particularly distinguishes it from  other  types  of
       editors.
【常用選項:】
        -n∶使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN的資料一般都會被列出到螢幕上。但如果加上 -n 參數 後,則只有經過sed 特殊處理的那一行(或者動作)纔會被列出來。
        -e∶直接在指令列模式上進行 sed 的動作編輯;
        -f∶直接將 sed 的動作寫在一個檔案內, -f filename 則可以執行 filename 內的sed 動作;
        -r∶sed 的動作支援的是延伸型正規表示法的語法。(預設是基礎正規表示法語法)
        -i∶直接修改讀取的檔案內容,而不是由螢幕輸出。
        -e∶直接在指令列模式上進行 sed 的動作編輯;
        -f∶直接將 sed 的動作寫在一個檔案內, -f filename 則可以執行 filename 內的sed 動作;
        -r∶sed 的動作支援的是延伸型正規表示法的語法。(預設是基礎正規表示法語法)
        -i∶直接修改讀取的檔案內容,而不是由螢幕輸出。       
【常用命令:】
        a   ∶新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)~
        c   ∶取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行!
        d   ∶刪除,因爲是刪除啊,所以 d 後面通常不接任何咚咚;
         i   ∶插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);
         p  ∶列印,亦即將某個選擇的資料印出。通常 p 會與參數 sed -n 一起運作~
         s  ∶取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法
        c   ∶取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行!
        d   ∶刪除,因爲是刪除啊,所以 d 後面通常不接任何咚咚;
         i   ∶插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);
         p  ∶列印,亦即將某個選擇的資料印出。通常 p 會與參數 sed -n 一起運作~
         s  ∶取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法
【案例】
【案例文本的內容】
[root@localhost test]# cat test 
111
222
333
444
555
666
777
$$
【顯示操作】
[root@localhost test]# sed -n '1p' test #顯示第1行 
111
[root@localhost test]# sed -n '$p' test #顯示最後1行
$$
[root@localhost test]# sed -n '1,3p' test   #顯示第1行到第3行
111
222
333
[root@localhost test]# sed -n '6,$p' test #顯示第6行到最後1行
666
777
$$
[root@localhost test]# sed -n '/3/p' test 
333
[root@localhost test]# sed -n '/\$/p' test 

【刪除操作】
[root@localhost test]# sed '1d' test #刪除第1行 
222
333
444
...
[root@localhost test]# sed '$d' test #刪除最後1行
111
222
333
...
[root@localhost test]# sed '1,3d' test #刪除第1行到第3行
444
555
666
...
[root@localhost test]# sed '6,$d' test  #刪除第6行到最後1行
111
222
...
【使用模式進行查詢】
[root@localhost test]# sed -n '/3/p' test  #查詢包括關鍵字3所在所有行
333
[root@localhost test]# sed -n '/\$/p' test #查詢包括關鍵字$所在所有行,使用反斜線\屏蔽特殊含義
$$
【增加一行或多行字符串】
[root@localhost test]# sed '1a add first content' test #第一行後增加字符串" add first content"
111
add first content
222
...

[root@localhost test]# sed '1,3a add first content' test  #第一行到第三行後增加字符串"drink tea"
[root@localhost test]# sed '1,3a add first content\nadd send content' test    #第一行後增加多行,使用換行符\n
【代替一行或多行】
[root@localhost test]# sed '1c change' test  #第一行代替爲change
change
222
...
【替換操作】
格式:sed 's#要替換的字符串#新的字符串#g'   (要替換的字符串可以用正則表達式)
[root@localhost test]# sed 's#111#xxx#g' test #替換111爲xxx
xxx
222
...
【插入操作】
 [root@localhost test]# sed -i '$a yyy' test          #在文件ab中最後一行直接輸入"bye"
【刪除匹配行操作】
sed -i '/匹配字符串/d'  filename  (注:若匹配字符串是變量,則需要“”,而不是‘’。記得好像是)
替換匹配行中的某個字符串
 sed -i '/匹配字符串/s/替換源字符串/替換目標字符串/g' filename
[root@localhost test]# sed -i '/111/d' test 






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