shell指令碼之sed工具使用

2021-10-01 07:22:37 字數 4189 閱讀 2670

執行

顯示

sed[選項]

'操作' 引數

sed[選項] -f 指令碼檔案 引數

3.3.1、p - 輸出符合條件的文字

[root@localhost ~]

# sed -n 'p' test.txt '輸出所有內容,等同於 cat test.txt'

...省略內容

[root@localhost ~]

# sed -n '3p' test.txt '輸出第 3 行'

...省略內容

[root@localhost ~]

# sed -n '3,5p' test.txt '輸出第 3~5 行'

...省略內容

[root@localhost ~]

# sed -n 'p;n' test.txt '輸出所有奇數行,n 表示讀入下一行資料'

...省略內容

[root@localhost ~]

# sed -n 'n;p' test.txt '輸出所有偶數行,n 表示讀入下一行資料'

...省略內容

[root@localhost ~]

# sed -n '1,5' test.txt '輸出第 1~5 行之間的奇數行(第 1、3、5 行)'

...省略內容

[root@localhost ~]

# sed -n '10,$' test.txt '輸出第 10 行至檔案尾之間的偶數行'

...省略內容

sed 命令與正規表示式結合使用

[root@localhost ~]

# sed -n '/the/p' test.txt '輸出包含the 的行'

...省略部分內容

[root@localhost ~]

# sed -n '4,/the/p' test.txt '輸出從第 4 行至第乙個包含 the 的行'

...省略部分內容

[root@localhost ~]

# sed -n '/the/=' test.txt '輸出包含the 的行所在的行號,等號(=)用來輸出行號'

...省略部分內容

[root@localhost ~]

# sed -n '/^pi/p' test.txt '輸出以pi 開頭的行'

...省略部分內容

[root@localhost ~]

# sed -n '/[0-9]$/p' test.txt '輸出以數字結尾的行'

...省略部分內容

[root@localhost ~]

# sed -n '/\/p' test.txt '輸出包含單詞wood 的行,\代表單詞邊界'

...省略部分內容

3.3.2、d - 刪除符合條件的文字

下面命令中 nl 命令用於計算檔案的行數,結合該命令可以更加直觀地檢視到命令執行的結果

[root@localhost ~]

# nl test.txt | sed '3d' '刪除第 3 行'

...省略內容

[root@localhost ~]

# nl test.txt | sed '3,5d' '刪除第 3~5 行'

...省略內容

[root@localhost ~]

# nl test.txt | sed '/cross/d' '刪除包含cross 的行'

...省略內容

[root@localhost ~]

# nl test.txt | sed '/cross/! d' '刪除不包含cross 的行'

...省略內容

[root@localhost ~]

# sed '/^[a-z]/d' test.txt '刪除以小寫字母開頭的行'

...省略內容

[root@localhost ~]

# sed '/\.$/d' test.txt '刪除以"."結尾的行'

...省略內容

[root@localhost ~]

# sed '/^$/d' test.txt '刪除所有空行'

...省略內容

[root@localhost ~]

# sed -e '/^$/' test.txt '刪除重複的空行,即連續的空行只保留乙個,效果與「cat -s test.txt」相同,n 表示讀下一行資料'

...省略內容

3.3.3、s - 替換符合條件的文字
[root@localhost ~]

# sed 's/the/the/' test.txt '將每行中的第乙個the 替換為 the '

[root@localhost ~]

# sed 's/l/l/2' test.txt '將每行中的第 2 個l 替換為l '

[root@localhost ~]

# sed 's/the/the/g' test.txt '將檔案中的所有the 替換為the'

[root@localhost ~]

# sed 's/o//g' test.txt '將檔案中的所有o 刪除(替換為空串)'

[root@localhost ~]

# sed 's/^/#/' test.txt '在每行行首插入#號'

[root@localhost ~]

# sed '/the/s/^/#/' test.txt '在包含the 的每行行首插入#號'

[root@localhost ~]

# sed 's/$/eof/' test.txt '在每行行尾插入字串eof'

[root@localhost ~]

# sed '3,5s/the/the/g' test.txt '將第 3~5 行中的所有the 替換為 the'

[root@localhost ~]

# sed '/the/s/o/o/g' test.txt '將包含the 的所有行中的o 都替換為 o'

3.3.4、遷移符合條件的文字
[root@localhost ~]

# sed '/the/;$g' test.txt '將包含the 的行遷移至檔案末尾,用於多個操作'

[root@localhost ~]

# sed '1,5;17g' test.txt '將第 1~5 行內容轉移至第 17 行後'

[root@localhost ~]

# sed '/the/w out.file' test.txt '將包含the 的行另存為檔案out.file '

[root@localhost ~]

# sed '/the/r /etc/hostname' test.txt '將檔案/etc/hostname 的內容新增到包含the 的每行以後'

[root@localhost ~]

# sed '3anew' test.txt '在第 3 行後插入乙個新行,內容為 new '

[root@localhost ~]

# sed '/the/anew' test.txt '在包含the 的每行後插入乙個新行,內容為 new'

[root@localhost ~]

# sed '3anew1\nnew2' test.txt '在第 3 行後插入多行內容,中間的\n 表示換行'

3.3.5、f - 使用指令碼編輯檔案
[root@localhost ~]

# sed '1,5;17g' test.txt '將第 1~5 行內容轉移至第 17 行後'

[root@localhost ~]

# vim abc.list '編輯指令放到/abc.list中'

1,5h

1,5d

17g[root@localhost ~]

# sed -f abc.list test.txt '使用abc.list檔案指令編輯test.txt檔案'

shell指令碼之sed工具使用

二 sed命令常見用法 sed 選項 操作 引數 sed 選項 f 指令碼檔案 引數選項 解釋 e或一expression 表示用指定命令或者指令碼來處理輸入的文字檔案 f或 file 表示用指定的指令碼檔案來處理輸入的文字檔案 h或 help 顯示幫助 n quiet或silent 表示僅顯示處理...

shell指令碼工具之sed命令

sed就是批處理的流編輯器,可以對來自檔案或標準輸入的輸入流進行轉換,sed通常被用作管道中的過濾器.由於sed僅僅對其輸入進行一遍掃瞄,因此比其它互動式編輯器更加高效.檔案內容 root tong1 opt cat sed.txt 1tong 2 cheng 3 hellow 4word wu h...

shell指令碼之sed開發

tmp file txt my qq num is 49000448.not4900000448.my god i am not oldbey,but clsn cheng my name is cheng.not oldman.my god i am not oldbey,but clsn i l...