sed摘錄note

聲明:以下是從網上搜的一些內容,比較雜亂,主要是方便以後查閱,如果能給您帶來方便那是最好不過了,如果您發現有侵權的地方,請您告訴我,我會刪除此帖。

 

主要是從以下地址摘錄的。

推薦閱讀原文。

http://bbs.chinaunix.net/thread-452942-1-1.html

 

s e d是一個非交互性文本流編輯器。它編輯文件或標準輸入導出的文本拷貝
" 抽取域。
" 匹配正則表達式。
" 比較域。
" 增加、附加、替換。
" 基本的s e d命令和一行腳本
sed與awk相同的是既可以用sed命令也可以寫成sed腳本,sed操作的只是一個拷貝,然後所有的改動如果沒有重定向到一個文件,將輸出到屏幕

sed怎樣讀取數據
s e d從文件的一個文本行或從標準輸入的幾種格式中讀取數據,將之拷貝到一個編輯緩衝區,然後讀命令行或腳本的第一條命令,並使用這些命令查找模式或定位行號編輯它。重複此過程直到命令結束

sed [選項] s e d命令 輸入文件
在命令行使用s e d命令時,實際命令要加單引號。s e d也允許加雙引號
sed [選項] -f sed腳本文件 輸入文件
sed -f myscript.sed input_file
s e d腳本文件[選項] 輸入文件

保存sed輸出
sed ‘some-sed-commands’ input-file > myoutfile

2.2 使用sed在文件中查詢文本的方式
1) 使用行號,可以是一個簡單數字,或是一個行號範圍。
2 ) 使用正則表達式

sed編輯命令

p 打印匹配行
= 顯示文件行號
a / 在定位行號後附加新文本信息
i / 在定位行號後插入新文本信息
d 刪除定位行
c / 用新文本替換定位文本
s 使用替換模式替換相應模式
r 從另一個文件中讀文本
w 寫文本到一個文件
q 第一個模式匹配完成後推出或立即推出
l 顯示與八進制A S C I I代碼等價的控制字符
{ } 在定位行執行的命令組
n 從另一個文件中讀文本下一行,並附加在下一行
g 將模式2粘貼到/pattern n/
y 傳送字符
n 延續到下一輸入行;允許跨行的模式匹配語句

sed和正則表達式

s e d識別任何基本正則表達式和模式及其行匹配規則。記住規則之一是:如果要定位一特殊字符,必須使用( /)屏蔽其特殊含義

 

替換文本
[ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]
g 缺省情況下只替換第一次出現模式,使用g選項替換全局所有出現模式。
p 缺省s e d將所有被替換行寫入標準輸出,加p選項將使- n選項無效。
- n選項不打印輸出結果。
w 文件名使用此選項將輸出定向到一個文件。

 

下面是一些一行命令集。([ ]表示空格, [ ]表示t a b鍵)
‘s / / . $ / / g’ 刪除以句點結尾行
‘-e /abcd/d’ 刪除包含a b c d的行
‘s / [ ] [ ] [ ] * / [ ] / g’ 刪除一個以上空格,用一個空格代替
‘s / ^ [ ] [ ] * / / g’ 刪除行首空格
‘s / / . [ ] [ ] * / [ ] / g’ 刪除句點後跟兩個或更多空格,代之以一個空格
‘/ ^ $ / d’ 刪除空行
‘s / ^ . / / g’ 刪除第一個字符
‘s /CO L / ( . . . / ) / / g’ 刪除緊跟C O L的後三個字母
‘s / ^ / / / / g’ 從路徑中刪除第一個/
‘s / [ ] / [ ] / / g’ 刪除所有空格並用t a b鍵替代
‘S / ^ [ ] / / g’ 刪除行首所有t a b鍵
‘s / [ ] * / / g’ 刪除所有t a b鍵

 

表達式 描述
/./ 將與包含至少一個字符的任何行匹配
/../ 將與包含至少兩個字符的任何行匹配
/^#/ 將與以 ‘#’ 開始的任何行匹配
/^$/ 將與所有空行匹配
/}^/ 將與以 ’}’(無空格)結束的任何行匹配
/} *^/ 將與以 ‘}’ 後面跟有零或多個空格結束的任何行匹配
/[abc]/ 將與包含小寫 ’a’、’b’ 或 ‘c’ 的任何行匹配
/^[abc]/ 將與以 ’a’、’b’ 或 ’c’開始的任何行匹配

 

字符類 描述
[] 字母數字 [a-z A-Z 0-9]
[] 字母 [a-z A-Z]
[] 空格或製表鍵
[] 任何控制字符
[] 數字 [0-9]
[] 任何可視字符(無空格)
[] 小寫 [a-z]
[] 非控制字符
[] 標點字符
[] 空格
[] 大寫 [A-Z]
[] 十六進制數字 [0-9 a-f A-F]

 

 

新版本的sed(GNU sed version 4.0.5)帶有-i選項,允許直接編輯修改文件內容而不需要重定向到臨時文件
如:a.txt:
old
aaa

sed -i ‘s/old/new/’ a.txt

cat a.txt
new
abc

quote.txt:
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

sed -n ‘2p’ quote.txt
sed -n ‘1,3p’ quote.txt
打印模式
sed -n ‘/The/p’ quote.txt

使用模式和行號進行查詢
sed -n ‘4,/The/p’ quote.txt
指定行找不到符合條件的,就從下一行開始查找,直到找到爲止,並把,找到行之前的全部打打印出來
如果指定行本身就符合條伯,把本行及後面的行的全部打印出來

匹配元字符
sed -n ‘//$/p’ quote.txt

顯示整個文件
sed -n ‘1,$p’ quote.txt

任意字符
sed -n ‘/.*ing/p’ quote.txt
首行
sed -n ‘1p’ quote.txt
最後一行
sed -n ‘$p’ quote.txt
打印行號
sed -e ‘/music/=’ quote.txt
sed -n ‘/music/=’ quote.txt
sed -n -e ‘/music/p’ -e ‘/music/=’ quote.txt

 

a.sed:
#!/bin/sed -f
/company/ a/Then suddenly it happed.

./a.sed quote.txt

sed “/company/i/Utter confusion followed.” quote.txt
或者
#!/bin/sed -f
4 i/Utter confusion followed.

#!/bin/sed -f
3 c/The office Dibble band played well.

#!/bin/sed -f

1 c/The Dibble band were grooving.
/evening/ i/They played some great tunes.
3 a/Where was the nurse to help?

刪除文本
[ a d d r e s s [,a d d r e s s ] ] d

sed ‘1d’ quote.txt
sed ‘1,3d’ quote.txt
sed ‘$d’ quote.txt
sed ‘/Neave/d’ quote.txt

替換文本
[ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]
g 缺省情況下只替換第一次出現模式,使用g選項替換全局所有出現模式。
p 缺省s e d將所有被替換行寫入標準輸出,加p選項將使- n選項無效。
- n選項不打印輸出結果。
w 文件名使用此選項將輸出定向到一個文件。

sed ‘s/night/NIGHT/’ quote.txt
sed ‘s//$//’ quote.txt
sed ‘s/The/Wow!/g’ quote.txt
sed ‘s/splendid/SPLENDID/w sed.out’ quote.txt

使用替換修改字符串
sed -n ‘s/nurse/“hello” &/p’ quote.txt
sed -n ‘s/played/from Hockering &/p’ quote.txt

將sed結果寫入文件命令
[ a d d r e s s [,address]]w filename
sed ‘1,2 w filedt’ quote.txt
sed ‘/Neave/ w dht’ quote.txt

從文件中讀文本
address r filename
echo “Boom boom went the music” >sedex.txt
sed ‘/company./r sedex.txt’ quote.txt

匹配後退出
address q
sed ‘/.a.*/q’ quote.txt

顯示文件中的控制字符
sed -n ‘1,$l’ dos.txt

處理控制字符
sed ‘s/##/ /g’ dos.txt
刪除所有行首的0。使用^符號表示模式從行首開始, ^ 0 表示行首任意個0。模式s / ^ 0 * / / g設置替換部分爲空,即爲刪除模式
sed ’s/##/ /g;s/^0
/ /g’ dos.txt
最後去除行尾^ M符號,爲此需做全局替換。設置替換部分爲空
sed ‘s/##/ /g;s/^0*/ /g;s/^M/ /g’ dos.txt

cat dos.txt | sed ‘s/^0*/ /g’ | sed ‘s/^M/ /g’ | sed ‘s/##/ /g’

1) 使用s / – * / / g刪除橫線- – - – - -。
2) 使用/ ^ $ / d刪除空行。
3) 使用$ d刪除最後一行
4) 使用1 d刪除第一行。
5) 使用awk {print $1}打印第一列。

cat data.txt |sed ‘s/—*/ /g’ | sed ‘/^$/d’ | sed ‘$d’ | sed ‘1d’ | awk ‘{print $1}’

sed ‘s/[0-9][0-9]*/& Passed/g’ ok.txt

從shell向sed傳值
[sam@chenwy sam]$ NAME=“It’s a go situation”
[sam@chenwy sam]$ REPLACE=“GO”
[sam@chenwy sam]$ echo $NAME | sed “s/go/$REPLACE/g”

從sed輸出中設置shell變量
[sam@chenwy sam]$ NAME=“It’s a go situation”
[sam@chenwy sam]$ REPLACE=“GO”
[sam@chenwy sam]$ NEW_NAME=`echo $NAME | sed “s/go/$REPLACE/g”`
[sam@chenwy sam]$ echo $NEW_NAME

下面是一些一行命令集。([ ]表示空格, [ ]表示t a b鍵)
‘s / / . $ / / g’ 刪除以句點結尾行
‘-e /abcd/d’ 刪除包含a b c d的行
‘s / [ ] [ ] [ ] * / [ ] / g’ 刪除一個以上空格,用一個空格代替
‘s / ^ [ ] [ ] * / / g’ 刪除行首空格
‘s / / . [ ] [ ] * / [ ] / g’ 刪除句點後跟兩個或更多空格,代之以一個空格
‘/ ^ $ / d’ 刪除空行
‘s / ^ . / / g’ 刪除第一個字符
‘s /CO L / ( . . . / ) / / g’ 刪除緊跟C O L的後三個字母
‘s / ^ / / / / g’ 從路徑中刪除第一個/
‘s / [ ] / [ ] / / g’ 刪除所有空格並用t a b鍵替代
‘S / ^ [ ] / / g’ 刪除行首所有t a b鍵
‘s / [ ] * / / g’ 刪除所有t a b鍵

刪除第一個/:
echo $PWD |sed ‘s/^////g’

將"Mr Wi l l i s “字串返回給s e d並在M r後而追加” B r u c e "。
echo “Mr Willis” |sed ‘s/Mr /& Bruce/g’

e d刪除字符串“a c c o u n t s . d o c”首字符

echo “accounts.doc” |sed ‘s/^.//g’

s e d刪除“a c c o u n t s . d o c”文件擴展名
echo “accounts.doc”|sed ‘s/.doc//g’

增加文件擴展名
echo “accounts”|sed ‘s/$/.doc/g’

替換字符系列
x=“Department+playroll&Building G”
echo $x |sed ‘s//+/ of /g’ |sed ‘s//&/ Located at /g’

sed -e ‘1d’ /etc/services | more
sed -e ‘1,10d’ /etc/services | more
sed -e ‘/^#/d’ /etc/services | more

規則
表達式 描述
/./ 將與包含至少一個字符的任何行匹配
/../ 將與包含至少兩個字符的任何行匹配
/^#/ 將與以 ‘#’ 開始的任何行匹配
/^$/ 將與所有空行匹配
/}^/ 將與以 ’}’(無空格)結束的任何行匹配
/} *^/ 將與以 ‘}’ 後面跟有零或多個空格結束的任何行匹配
/[abc]/ 將與包含小寫 ’a’、’b’ 或 ‘c’ 的任何行匹配
/^[abc]/ 將與以 ’a’、’b’ 或 ’c’開始的任何行匹配

sed -e ‘/^#/d’ /etc/services | more

sed -n -e ‘/^#/p’ /path/to/my/test/file | more
sed -n -e ‘/BEGIN/,/^END/p’ /my/test/file | more
sed -n -e ‘/main[[:space:]]*(/,/^}/p’ sourcefile.c | more
替換!
sed -e ‘s/foo/bar/’ myfile.txt
sed -e ‘s/foo/bar/g’ myfile.txt
sed -e ‘1,10s/enchantment/entrapment/g’ myfile2.txt
sed -e ‘/^$/,/^END/s/hills/mountains/g’ myfile3.txt
sed -e ‘s:/usr/local:/usr:g’ mylist.txt
sed -e ‘s/usr/local/usrg’ mylist.txt

規則表達式混亂
sed -e ‘s/<.*>//g’ myfile.html
sed -e ‘s/<[^>]*>//g’ myfile.html

‘[a-x]*’
這將匹配零或多個全部爲 ’a’、’b’、’c’…’v’、’w’、’x’ 的字符。另外,可以使用 ‘[]’ 字符類來匹配空格。以下是可用字符類的相當完整的列表:

字符類 描述
[] 字母數字 [a-z A-Z 0-9]
[] 字母 [a-z A-Z]
[] 空格或製表鍵
[] 任何控制字符
[] 數字 [0-9]
[] 任何可視字符(無空格)
[] 小寫 [a-z]
[] 非控制字符
[] 標點字符
[] 空格
[] 大寫 [A-Z]
[] 十六進制數字 [0-9 a-f A-F]

 

高級替換功能

組合使用
sed -n -e ‘=;p’ myfile.txt
sed -n -e ‘=’ -e ‘p’ myfile.txt
sed -n -f mycommands.sed myfile.txt

sed -e ‘s/$//r/’ myunix.txt > mydos.txt sed -e ‘s/.$//’ mydos.txt > myunix.txt

 

1d /^^/d s/[[]]//g /^D/ {
s/^D/(./)//1/tOUTY/tINNY/t/
s/^01/Jan/ s/^02/Feb/
s/^03/Mar/ s/^04/Apr/
s/^05/May/ s/^06/Jun/
s/^07/Jul/ s/^08/Aug/
s/^09/Sep/ s/^10/Oct/
s/^11/Nov/ s/^12/Dec/
s:^/(.
/)//(./)//(./):/2 /1 /3: }

1d /^^/d s/[[]]//g /^D/ {
s/^D/(./)//1/tOUTY/tINNY/t/
s/^01/Jan/ s/^02/Feb/
s/^03/Mar/ s/^04/Apr/
s/^05/May/ s/^06/Jun/
s/^07/Jul/ s/^08/Aug/
s/^09/Sep/ s/^10/Oct/
s/^11/Nov/ s/^12/Dec/
s:^/(.
/)//(./)//(./):/2 /1 /3:
N N N
s//nT/(./)/nN/(./)/nP/(./)/NUM/2NUM/t/tY/t/t/3/tAMT/1AMT/
s/NUMNUM/-/ s/NUM/([0-9]
/)NUM//1/
s//([0-9]/),//1/ }

s//nT/(./)/nN/(./)/nP/(.*/)/NUM/2NUM/t/tY/t/t/3/tAMT/1AMT/

最終的“QIF 到文本”腳本 1d /^^/d s/[[]]//g /^D/ { s/^D/(./)//1/tOUTY/tINNY/t/
s/^01/Jan/ s/^02/Feb/ s/^03/Mar/ s/^04/Apr/ s/^05/May/
s/^06/Jun/ s/^07/Jul/ s/^08/Aug/ s/^09/Sep/ s/^10/Oct/
s/^11/Nov/ s/^12/Dec/ s:^/(.
/)//(./)//(./):/2 /1 /3:
N N N s//nT/(./)/nN/(./)/nP/(./)/NUM/2NUM/t/tY/t/t/3/tAMT/1AMT/
s/NUMNUM/-/ s/NUM/([0-9]
/)NUM//1/ s//([0-9]/),//1/
/AMT-[0-9].[0-9]AMT/b fixnegs
s/AMT/(./)AMT//1/ s/OUTY/-/ s/INNY/inco/
b done :fixnegs s/AMT-/(.
/)AMT//1/ s/OUTY/misc/
s/INNY/-/ :done }

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