sed命令基本用法

2021-06-25 09:43:57 字數 4342 閱讀 2989

sed是乙個飛互動式文字編輯器

sed只對緩衝區中原始檔案的副本進行編輯,並不編輯原始的檔案

呼叫sed的方法:

①在shell命令列輸入命令呼叫sed,格式為:

sed [選項] 'sed命令' 輸入檔案

sed [選項] -f sed指令碼檔案 輸入檔案
./sed 指令碼檔案 輸入檔案
第③種方式的sed指令碼檔案與第②種有所不同,其sed指令碼檔案需要以sha-bang(#!)符號開頭,sha-bang後面是解析這個指令碼的程式名。

sed的常用選項及其意義

選      項

意      義

示      例

-n不列印所有行到標準輸出

見 n.1 n.2 n.3

-e[root@localhost ~]# sed -n -e '/certificate/p' -e '/certificate/=' input

-f表示正在呼叫sed指令碼檔案

/file:/a\ #a\表示在此處換行新增文字

sed命令通常由定位問本行和sed編輯命令兩部分組成

sed命令定位文字的方法

選      項

意      義

示      例

xx為指定行號

n.1 [root@localhost ~]# sed -n '1p' input

x,y指定從x到y的行號範圍

n.2 [root@localhost ~]# sed -n '3,6p' input

/pattern/

查詢包含模式的行

n.3 [root@localhost ~]# sed -n '/certificate/p' input

/pattern/pattern/

查詢包含兩個模式的行

/pattern/,x

從與pattern的匹配行到x號行之間的行

[root@localhost ~]# sed -n '/seugrid/,$p' input

x,/pattern/

從x號行到與pattern的匹配行之間的行

[root@localhost ~]# sed -n '3,/seugeid/p' input

x,y!

查詢不包括x和y行號的行

[root@localhost ~]# sed -n '2,10!p' input

sed編輯命令

選      項

意      義

示      例

p列印匹配行

[root@localhost ~]# sed -n '/certificate/p' input

=列印檔案行號

[root@localhost ~]# sed -n '/certificate/=' input

a\在定位行號之後追加文字資訊

i\在定位行號之前插入文字資訊

[root@localhost ~]# vim insert.sed

#!/bin/sed -f

/file:/i\

we insert a new line.

[root@localhost ~]# chmod u+x insert.sed

[root@localhost ~]# ./insert.sed input

d刪除定位行

[root@localhost ~]# sed '1d' input     #刪除第一行

[root@localhost ~]# sed '$d' input     #刪除最後一行

[root@localhost ~]# sed '1,10d' input     #刪除第1~10行

[root@localhost ~]# vim delete.sed

#!/bin/sed -f

/[cc][ee][rr][tt][ii][ff][ii][cc][aa][tt][ee]/d

[root@localhost ~]# chmod u+x delete.sed

[root@localhost ~]# ./delete.sed input

c\用新文字替換定位文字

[root@localhost ~]# vim modify.sed

#!/bin/sed -f

/file:/c\

we modify this line.

[root@localhost ~]# chmod u+x modify.sed

[root@localhost ~]# ./modify.sed input

s使用替換模式替換相應模式

[root@localhost ~]# sed 's/certificate/certificate/' input

[root@localhost ~]# sed -n 's/certificate/certificate/p' input     #-n和-p選項結合使用,只列印替換行

[root@localhost ~]# sed -n 's/certificate/certificate/' input     #少了p選項,不列印任何內容

[root@localhost ~]# sed -n 's/seu/njue/p' input     #不帶g選項的結果

[root@localhost ~]# sed -n 's/seu/njue/' input     #帶g選項的結果

[root@localhost ~]# sed -n 's/seu/njue/2p' input     #sed替換第n次匹配

[root@localhost ~]# sed -n 's/seu/njue/w outpout' input     #將sed字串改為njue,並將結果寫入output檔案

[root@localhost ~]# sed -n 's/seu/(&)/pg' input     #&表示了seu

r從另乙個檔案中讀文字

[root@localhost ~]# vim otherfile

this is the first line of the otherfile.

this is the second line of the otherfile.

[root@localhost ~]# sed '/certificate/r otherfile' input     #在與certificate匹配的行後讀入otherfile檔案

w將文字寫入到乙個檔案

[root@localhost ~]# sed -n '1,5 w output' input     #將1~5行寫入output檔案

[root@localhost ~]# sed -n '/globus/w output' input     #將與goobus關鍵字匹配的行寫入output檔案

y交換字元

[root@localhost ~]# sed 'y/fmj/fmj/' input

q第乙個模式匹配完成後退出

[root@localhost ~]# sed '5 q' input     #列印前5行,然後退出

[root@localhost ~]# sed '/.r.*/q' input     #匹配第1個字串後立即退出

l顯示與八進位制ascii碼等價的控制字元

[root@localhost ~]# sed -n '1,$l' control

{}在定位行執行的命令組

[root@localhost ~]# sed -n '/certificate/' input     #列印與certificate匹配行的內容及行號

[root@localhost ~]# sed '/certificate/' input     #在與certificate關鍵字匹配行將全部的i替換為i、將第1個le替換為99

n讀取下乙個輸入行,用下乙個命令處理新的行

[root@localhost ~]# sed '/certificate/' input

h將模式緩衝區的文字複製到保持緩衝區

[root@localhost ~]# sed '/subject/h' input

h將模式緩衝區的文字追加到保持緩衝區

[root@localhost ~]# sed '/seugrid/h' input

x互換模式緩衝區和保持緩衝區的內容

[root@localhost ~]# sed '/seugrid/x' input

g將保持緩衝區的內容複製到模式緩衝區

g將保持緩衝區的內容追加到模式緩衝區

[root@localhost ~]# sed '$g' input

sed命令基本用法

1 sed stream editor 行編輯器。工作原理 預設不編輯原始檔,僅僅對模式空間的資料做處理。一行行將檔案讀入模式空間,處理完後再將模式空間的內容輸出一遍。sed和grep awk編輯器都是shell程式設計中的重要工具。2 sed語法 sed option addresscommand...

sed命令基本用法 vbird

usage sed nefr 動作 n 使用安靜模式,在一般sed的用法中,所有來自stddin的資料一般都會被出到螢幕上,但如果加上 n引數,則只有經過sed特殊處理的那一行才會被列出來。e 直接在命令列模型上進行sed的動作編輯 f 直接將sed的動作寫在乙個檔案內,f filename則可能執...

sed命令的基本用法

sed stream editor 是流編輯器,可對文字檔案和標準輸入進行編輯。sed只是對快取區中的原始檔案的副本進行編輯,並不編輯原始的檔案,如果需要儲存改動內容,可以選擇使用重定向和w編輯命令。呼叫sed有三種方法 1.在shell命令列輸入命令呼叫sed,格式為 sed 選項 sed命令 輸...