sed基本使用

來源:互聯網
上載者:User

標籤:sed

sed [options] sed-commands input-file

options:

-n: 靜默模式,不輸出模式空間中的內容

-e script –e script: 指定多個指令碼命令

-f script_file: 指定指令檔

-i: 直接編輯原檔案

-r: 使用擴充的Regex

 

sed-commands:

d: 刪除

p: 列印

i \text: 在模式比對行的前面插入

a \text: 在模式比對行的後面追加

r /path/to/somefile: 在指定位置讀取另外一個檔案插入

w /path/to/somefile: 把合格所有行儲存至指定的檔案中

=: 列印行號

s/尋找條件/替換文本/: 尋找替換

 

地址定界:

1,3: 匹配第一行到第三行

2,$:  匹配第二行到最後一行

/he/,$: 匹配he所在的行到最後一行

/he/,/yy/:匹配he所在的行到yy所在的行

/li/: 匹配所有li的行


sed尋找替換命令格式:

    sed ‘[address-range | pattern-range] s/original-string/replacement-string/[flags]‘ input-file

    flags:

        g: 替換一行中所有匹配的模式

        [1|2|3|..n]: 規劃換一行中第n次匹配的模式

        p: 列印替換後的模式

        w output.txt: 把替換後的文本寫入檔案output.txt

        i: 忽略搜尋的模式的大小寫

        

sed的工作模式:

    1.讀取一行到模式空間(模式空間是一個sed命令的buffer空間)

    2.在此模式空間中執行sed 命令

    3.列印模式空間中的行

    4.重複以上步驟


執行個體:

     以employee.txt為input-file

     

    # vim employee.txt     101,John Doe,CEO    102,Jason Smith,IT Manager    103,Raj Reddy,Sysadmin    104,Anand Ram,Developer    105,Jane Miller,Sales Manager

     

     列印所有行

# sed -n ‘p‘ employee.txt 101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager


從檔案中讀取sed-commands

# vim script.sed /admin/ p/IT/ p# sed -n -f script.sed employee.txt 102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin


使用-e指定多個sed-commands(方法一)

# sed -n -e ‘/admin/p‘ -e ‘/IT/p‘ employee.txt 102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin


使用-e指定多個sed-commands(方法二)

# sed -n > -e ‘/admin/p‘ > -e ‘/IT/p‘ > employee.txt 102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin


使用-e指定多個sed-commands(方法二)


列印第二行

# sed -n ‘2 p‘ employee.txt 102,Jason Smith,IT Manager


列印第一行到第四行

# sed -n ‘1,4 p‘ employee.txt  101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer


列印第二行到最後一行

# sed -n ‘2,$ p‘ employee.txt    102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager


列印從第一行開始的2行

# sed -n ‘1,+2 p‘ employee.txt  101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin


列印奇數行

# sed -n ‘1~2 p‘ employee.txt    101,John Doe,CEO103,Raj Reddy,Sysadmin105,Jane Miller,Sales Manager


列印偶數行

# sed -n ‘2~2 p‘ employee.txt  102,Jason Smith,IT Manager104,Anand Ram,Developer


刪除第二行

# sed ‘2 d‘ employee.txt 101,John Doe,CEO103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager


刪除第三行到最後一行

# sed ‘3,$ d‘ employee.txt 101,John Doe,CEO102,Jason Smith,IT Manager


刪除空行

# sed ‘/^$/ d‘ employee.txt


刪除以#開始的注釋行

sed ‘/^#/ d‘ employee.txt


把employee.txt的偶數行寫入到output.txt

# sed -n ‘2 w output.txt‘ employee.txt # cat output.txt102,Jason Smith,IT Manager


把Manager改成Director

# sed ‘s/Manager/Director/‘ employee.txt 101,John Doe,CEO102,Jason Smith,IT Director103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Director


搜尋Sales所在行的Manager為Director

# sed ‘/Sales/s/Manager/Director/‘ employee.txt 101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Director


搜尋替換一行中所有的小寫字母a為大寫字母A

# sed ‘s/a/A/g‘ employee.txt 101,John Doe,CEO102,JAson Smith,IT MAnAger103,RAj Reddy,SysAdmin104,AnAnd RAm,Developer105,JAne Miller,SAles MAnAger


搜尋替換一行中第二個出現的小寫字母a為大寫字母A

[[email protected] ~]# sed ‘s/a/A/2‘ employee.txt  101,John Doe,CEO102,Jason Smith,IT MAnager103,Raj Reddy,SysAdmin104,Anand RAm,Developer105,Jane Miller,SAles Manager


搜尋包含John的行並替換為Jonny,僅列印此行

# sed -n ‘s/John/Jonny/p‘ employee.txt 101,Jonny Doe,CEO


搜尋包含John的行並替換為Jonny,寫入到檔案output.txt

# sed -n ‘s/John/Jonny/w output.txt‘ employee.txt  # cat output.txt 101,Jonny Doe,CEO


搜尋包含John的行(忽略大小寫)並替換為Jonny,寫入到檔案output.txt

# sed -n ‘s/john/Jonny/iw output.txt‘ employee.txt # cat output.txt 101,Jonny Doe,CEO


在第1到2行的前面的行首插入<<,行尾插入>>

# sed -e ‘1,2s/^/<< /‘ -e ‘1,2s/$/ >>/‘ employee.txt          << 101,John Doe,CEO >><< 102,Jason Smith,IT Manager >>


sed搜尋定界符也可使用以下幾種形式

sed ‘s|/usr/local/bin|/usr/bin|‘ input-filesed ‘s^/usr/local/bin^/usr/bin^‘ input-filesed ‘[email protected]/usr/local/[email protected]/usr/[email protected]‘ input-filesed ‘s!/usr/local/bin!/usr/bin!‘ input-file


&用於替換匹配的oritinal-string

# sed ‘s/^[0-9]\{3\}/[&]/g‘ employee.txt [101],John Doe,CEO[102],Jason Smith,IT Manager[103],Raj Reddy,Sysadmin[104],Anand Ram,Developer[105],Jane Miller,Sales Manager# sed -r ‘s/^[0-9]{3}/[&]/g‘ employee.txt   [101],John Doe,CEO[102],Jason Smith,IT Manager[103],Raj Reddy,Sysadmin[104],Anand Ram,Developer[105],Jane Miller,Sales Manager


單分組引用

# sed ‘s/\([^,]*\).*/\1/g‘ employee.txt   101102103104105


多分組引用

# sed ‘s/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/g‘ employee.txt 101,CEO102,IT Manager103,Sysadmin104,Developer105,Sales Manager


\l表示把後面的單個字元N視為小寫n,\L表示將後面的所有字元HNNY視為小寫hnny

# sed -n ‘s/John/JO\LHNNY/p‘ employee.txt  101,JOhnny Doe,CEO# sed -n ‘s/John/JO\lHNNY/p‘ employee.txt  101,JOhNNY Doe,CEO


\u表示把後面的單個字元n視為大寫N,\U表示將後面的所有字元hnny視為大寫HNNY

# sed -n ‘s/John/JO\uhnny/p‘ employee.txt        101,JOHnny Doe,CEO# sed -n ‘s/John/JO\Uhnny/p‘ employee.txt  101,JOHNNY Doe,CEO


\E和\L \U配合使用,作為定界符

# sed -n ‘s/John/JO\Uhnn\Ey/p‘ employee.txt    101,JOHNNy Doe,CEO


把\3改為大寫

# sed ‘s/\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\U\3\E/g‘ employee.txt 101,CEO102,IT MANAGER103,SYSADMIN104,DEVELOPER105,SALES MANAGER


把sed作為一個解譯器來用

# cat script.sed #!/bin/sed -fs/\([^,]*\),\([^,]*\),\([^,]*\).*/\2,\1,\3/gs/^.*/<&>/s/Developer/IT Manager/s/Manager/Director/# ./script.sed employee.txt <John Doe,101,CEO><Jason Smith,102,IT Director><Raj Reddy,103,Sysadmin><Anand Ram,104,IT Director><Jane Miller,105,Sales Director>


直接修改原檔案

# sed -i ‘s/John/Johnny/‘ employee.txt# cat employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager


直接修改原檔案,並備份原檔案

# sed -ibak ‘s/John/Johnny/‘ employee.txt                # ll employee.txt*-rw-r--r-- 1 root root 123 Sep 15 18:05 employee.txt-rw-r--r-- 1 root root 121 Sep 15 18:05 employee.txtbak


直接修改原檔案,並指定檔案尾碼名

# sed --in-place=.bak ‘s/John/Johnny/‘ employee.txt# ll employee.txt*-rw-r--r-- 1 root root 123 Sep 15 18:08 employee.txt-rw-r--r-- 1 root root 121 Sep 15 18:07 employee.txt.bak


追加a和插入i

# sed -e ‘1 i \Staff Information\n-------------‘ -e ‘$ a \END\n-------------‘ employee.txt   Staff Information-------------101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales ManagerEND-------------


替換c

# sed ‘/Sales/ c \105,Jane Miller,Sales Director‘ employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Director


列印隱藏字元

# sed -n ‘l‘ employee.txt101,Johnny Doe,CEO$102,Jason Smith,IT Manager$103,Raj Reddy,Sysadmin$104,Anand Ram,Developer$105,Jane Miller,Sales Manager$


列印行號

# sed ‘=‘ employee.txt1101,Johnny Doe,CEO2102,Jason Smith,IT Manager3103,Raj Reddy,Sysadmin4104,Anand Ram,Developer5105,Jane Miller,Sales Manager


轉換大小寫字母

# sed  ‘y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/‘ employee.txt 101,JOHNNY DOE,CEO102,JASON SMITH,IT MANAGER103,RAJ REDDY,SYSADMIN104,ANAND RAM,DEVELOPER105,JANE MILLER,SALES MANAGER


多個input-file

# sed -n ‘/^root\>/ p‘ /etc/{passwd,group}  root:x:0:0:root:/root:/bin/bashroot:x:0:


退出命令q

# sed ‘q‘ employee.txt101,Johnny Doe,CEO# sed ‘2q‘ employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager


命令n:用於列印當前模式空間,並從input-file中讀入下一行

# sed  ‘n‘ employee.txt  101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager



     

    

本文出自 “虎虎生威” 部落格,請務必保留此出處http://tobeone.blog.51cto.com/817917/1553001

sed基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.