Linux sed?戒護
Linux sed?戒護???╃??cript?ュ?????????浠躲??
sed??渚???cript????浠わ??ュ?????缂?杈???????浠躲??
Sed涓昏??ㄦ?ヨ???ㄧ?杈?涓?涓???澶?涓???浠訛?绠???瀵規??浠剁????澶???浣?锛?缂???杞??㈢?搴?绛???
璇?娉?
sed [-hnV][-e<script>][-f<script??浠?gt;][??????浠?
???拌?存??锛?
- -e<script>??--expression=<script> 浠ラ??椤逛腑??瀹???script?ュ???杈??ョ????????浠躲??
- -f<script??浠?gt;??--file=<script??浠?gt; 浠ラ??椤逛腑??瀹???script??浠舵?ュ???杈??ョ????????浠躲??
- -h??--help ?劇ず甯??┿??
- -n??--quiet??--silent 浠??劇ずscript澶???????缁?????
- -V??--version ?劇ず????淇℃????
- -i灏卞?扮?杈???浠?/li>
?ㄤ?璇存??锛?
- a 锛??闆?锛? a ?????㈠??浠ユ?ュ??涓詫???杩?浜?瀛?涓蹭??ㄦ?扮??涓?琛??虹????????涓?涓?琛?)锝?
- c 锛???浠o? c ?????㈠??浠ユ?ュ??涓詫?杩?浜?瀛?涓插??浠ュ??浠?n1,n2 涔??寸??琛?锛?
- d 锛????わ???涓烘?????ゅ??锛???浠?d ???㈤??甯鎬??ヤ換浣?????锛?
- i 锛????ワ? i ?????㈠??浠ユ?ュ??涓詫???杩?浜?瀛?涓蹭??ㄦ?扮??涓?琛??虹????????涓?涓?琛?)锛?
- p 锛????幫?浜??沖???涓????╃???版???闆?恒????甯?p 浼?涓?????sed -n 涓?璧瘋?琛?锝?
- s 锛???浠o???浠ョ?存?ヨ?琛???浠g??宸ヤ?锛???甯歌?涓? s ???ㄤ???浠ユ????姝h?琛ㄧず娉?锛?渚?濡? 1,20s/old/new/g 锛?
?
绀轟???妗?- sed_test.txt锛?
Every 2.0s: cat sed_test.txt Sun Mar 31 08:47:18 2019
This is one sed text.
I onem beautyful.
You onere not beautyful.
He is onelso not beautyful.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very good.
sed ?挎??
Sed ???挎?㈡??浠朵腑??瀛?涓層??璧???琛??????寵????恒???朵腑,琛ㄧず?挎?㈠??涓茬????浠や腑???芥?闆???頒負s;琛ㄧず?挎?㈣???琛?????璧?????gt;????浠や腑???芥?闆???頒負c??涓?杩版???典互涓????涓?渚?瀛?璇存????
*琛????挎??(1) sed -e '1c/#!/bin/more' file (??绗?涓?琛??挎?㈡??#!/bin/more)
??? ????: ??绗?n琛??挎?㈡??just do it
??? sed -e 'nc/just do it' file
(2) sed -e '1,10c/I can do it' file? (??1??0琛??挎?㈡??涓?琛?:I can do it)
??? ????: ?㈡??涓よ?(I can do it! Let's start)
??? sed -e '1,10c/I can do it!/nLet'"/'"'s start' file
*瀛?绗????挎??(3) sed -e 's/word1/& word2/' file (灏?姣?涓?琛???word1??璇??挎?㈡??s???版??澶?涓?涓や釜浣?缃????扮?哥???,?芥?闆????涓???涓や釜?規????绗???
??? & : 浠h〃pattern
??? /n : 浠h〃 pattern 涓?琚?绗? n 涓? /( ??/)(??????褰? A]) ????璧鋒?ョ??瀛?涓層??渚?濡?
??? sed -e 's/w1/& w2/' file? # w1???版?矽???w1 w2
??? sed -e? 's//(test/) /(my/) /(car/)/[/2 /3 /1]/' file?? #缁???: [my car test]
*flag ???頒婦渚?
??? sed -e 's/w1/& w2/g' file
??? g : 浠h〃?挎?㈡?????歸??椤圭??;杩???,??浠朵腑????瀛?绗?覆w1?戒?琚??挎?㈡??瀛?涓?1 w2
??? sed -e 's/w1/& w2/10' file
??? m(10) : ?挎?㈣???绗?m涓?绗?????瀛?涓? 璁頒?锛???琛?????绗?m涓??歸????瀛?涓???? sed -e 's/w1/& w2/p' file
??? p : ?挎?㈢??涓?涓???w1?歸????瀛?绗?覆涓?1 w2锛?骞惰??哄?版????杈???
??? sed -e 's/w1/& w2/w w2file' file
??? w filename : 璇ュ???頒?灏??挎?㈣?????瀹瑰???ュ?版??浠?2file骞惰??烘?挎?㈠?????翠釜??浠躲??娉ㄦ??w2file???????????挎?㈣???琛?????? sed 'e 's/w1/& w2/' file
??? 杩?????flag 涓虹┖, 杩??峰氨????灏?绗?涓?涓?w1?歸????瀛?绗?覆?挎?㈡??w1 w2?????㈢??涓?杩?琛??挎????
*浣?缃????闆??ㄤ婦渚?
??? sed -e '/machine/s/phi/beta/g' file
??? 灏???浠朵腑??"machine"瀛?涓茬??璧???琛?涓???"phi"瀛?涓??挎?㈡??涓?beta"瀛?涓???? sed -e '1,10 s/w1/& w2/g' file
??? ??1??0????w1瀛?绗?覆?挎?㈡??w1 w2瀛?绗?覆??
??? sed -e '1,/else/ s/w1/& w2/g' file
??? ??1?闆??绗?覆else????w1瀛?绗?覆?挎?㈡??w1 w2瀛?绗?覆??
?跺??浣?缃????扮??搴??ㄤ????㈢???稿????
sed -i 's/beautyful/chou/' sed_test.txt锛?
????????? beautyful ?挎??負 not beautyful
?煎?锛?
sed -i 's/琚??挎?㈠??瀹??挎?㈢????瀹?' ??????浠?/code>
# -i?
灏卞?扮?杈???浠?
# s???浠o???浠ョ?存?ヨ?琛???浠g??宸ヤ?
Every 2.0s: cat sed_test.txt Sun Mar 31 08:48:41 2019
This is one sed text.
I onem chou.
You onere not chou.
He is onelso not chou.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very good.
sed澶?????浠ょ??瀹??版?瑰?
锛?1锛?浣跨?ㄥ???風?擱??锛??
sed -i 's/chou/beautyful/;s/one/a/' sed_test.txt
This is a sed text.
Every 2.0s: cat sed_test.txt Sun Mar 31 09:00:00 2019
This is a sed text.
I am beautyful.
You are not beautyful.
He is also not beautyful.
Hei Hei.
Are you laugh?
No, you can not do that.
Are you sad?
Oh, It is very good.
锛?2锛??ㄦ??涓???浠ゅ???劇疆 -e锛??sed -i -e 's/beautyful/chou/' -e 's/a/one/' sed_test.txt?
Every 2.0s: cat sed_test.txt Sun Mar 31 09:01:20 2019
This is one sed text.
I onem chou.
You onere not chou.
He is onelso not chou.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very good.
锛?3锛?浣跨??hell????琛???浠ゅ????
[[email?protected] shell]# sed -i '
> s/chou/beautyful/
> s/one/a/' sed_test.txt
缁???锛?
This is a sed text.
Every 2.0s: cat sed_test.txt Sun Mar 31 09:06:15 2019
This is a sed text.
I am beautyful.
You are not beautyful.
He is also not beautyful.
Hei Hei.
Are you laugh?
No, you can not do that.
Are you sad?
Oh, It is very good.
sed ??????浠? 褰??戒護寰?灏????跺??锛???浠?????涓?杩扮?存?ュ?ㄥ?戒護琛?杈??ユ????琛?????浣???褰??戒護寰??跨???跺??锛???杈??ヨ?涔??跨????????涓?????瀹???????涓?杩?锛???浠???浠ラ??杩???寤鴻??????浠訛??ц??ц????戒護涔????ㄨ??????浠朵腑?沖????
??杩? -f ??椤規?ユ??瀹??戒護琛?涓?????????浠剁????瀛?锛?sed -f scriptfile file 锛?scriptfile ?崇???????????浠訛?file ?寵???浣?????????浠? 缂?????????浠訛?vim sed_script
?
[[email?protected] shell]# cat sed_script
s/beautyful/chou/
s/a/one/
s/good/very good/
?ц?锛?
sed -i -f sed_script sed_test.txt
?缁???锛?
Every 2.0s: cat sed_test.txt Sun Mar 31 09:15:49 2019
This is one sed text.
I onem chou.
You onere not chou.
He is onelso not chou.
Hei Hei.
Are you loneugh?
No, you conen not do that.
Are you soned?
Oh, It is very very good.
?
?sed榛?璁よ??烘??杈??哄?扮?绔?锛?濡????蟲??sed??杈??洪??瀹????闆???涓?绋?搴?涓?锛???瑕??ㄥ???㈡??瀹?涓?涓?I/O??瀹???绗??楓??
sed -f scriptfile file > newfile
?
?Linux涓??歸???挎?㈠?涓???浠朵腑??瀛?绗?覆??绠????規?????ed?戒護??浠ユ?歸???挎?㈠?涓???浠朵腑??瀛?绗?覆??
?戒護濡?涓?锛?
sed -i "s/??瀛?绗?覆/?闆??绗?覆/g" `grep ??瀛?绗?覆 -rl ???ㄧ??褰?`
渚?濡?锛???瑕??? charset=gb2312 ?挎??負 charset=UTF-8锛??ц??戒護锛?
sed -i "s/charset=gb2312/charset=UTF-8/g" `grep charset=gb2312 -rl /www`
?沖????
?
瑙i??涓?涓?锛?
-i 琛ㄧずinplace edit锛?灏卞?頒慨?規??浠?
-r 琛ㄧず??绱㈠????褰?
-l 琛ㄧず杈??哄?歸??????浠跺??
杩?涓??戒護缁???寰?寮哄ぇ锛?瑕?娉ㄦ??澶?浠芥??浠躲??
sed涓?y?戒護涓?s?戒護???哄??
棣???杩?涓や釜?戒護?藉??浠ョ?ㄤ??挎???浣??挎?㈡?惰??????哄????
锛?1锛?y涓?????琛?绾у?????挎???s涓???????绾у???挎???褰??朵???浠ヨ漿?㈡??琛?绾э?锛?
y锛?瀛??????挎??(1)sed -e 'y/abc../xyz../' filename
????浠朵腑??a瀛?姣??挎?㈡??x, b?挎?㈡??y, c?挎?㈡??z??
(2) sed -e 'y/abc/ABC' filename
??灏?????abc杞??㈡??澶у????ABC
锛?2锛?s?挎?㈢?????翠?锛?y?挎?㈢????姣?涓?瀛?姣?瀵瑰?????涓?瀛?姣?
?
渚?锛?
1??sed 's/dog/cat/' data ? ? ??data涓???????琛?涓???绗?涓?娆″?虹??og???挎?㈡??cat锛?锛?娉ㄦ??锛?濡???cat/??娌℃???虹?闆?朵?褰㈠?锛???榛?璁ゆ?剁??涓?娆″?虹?扮??浣?缃?锛?
2??sed '1,3y/abc/ABC/' data ?????data涓???绗?涓?琛??崇??涓?琛?涓???a?挎?㈡??A锛?b?挎?㈡??B锛?c?挎?㈡??C
3??sed '2,${s/dog/cat/1}' data ????data涓???绗?浜?琛??蟲????涓?琛???绗?涓?娆″?虹??og???挎?㈡??cat
?
娴?璇???妗o?
Every 2.0s: cat /mnt/sed/test_sed Sun Mar 31 09:33:50 2019
1234567890
2345678901
3456789012
4567890123
?ц?锛?
sed -i 'y/1234567890/ABCDEFGHIJ/' test_sed
?
?缁???锛?
Every 2.0s: cat /mnt/sed/test_sed Sun Mar 31 09:35:13 2019
ABCDEFGHIJ
BCDEFGHIJA
CDEFGHIJAB
DEFGHIJABC
娉ㄦ??锛? ???㈠?崇郴????涓や釜list??浣?缃?瀵瑰?????
print ??=================================================================================??
print ??=================================================================================??
???わ?d?戒護
(1) sed -e '1d' inputfile (???ょ??涓?琛?)
??? ?d????ょ??x琛??????ょ??x1,x2,x3琛????
??? sed -e 'xd' inputfile
??? sed -e 'x1d' -e 'x2d' -e 'x3d' inputfile
??? 褰??朵?璁歌????村ソ????娉???
(2) sed -e '1,3d' file (???ょ??涓??扮??涓?琛?)
??? ????锛????ょ??n琛??扮??m琛??涔?灏辨??
??? sed -e 'n,md' file
??? ???ょ??涓?琛??版????涓?琛?
??? sed -e '1,$d' file???? #$ ????涓?琛???涓?琛???????
(3) sed -e '/#/d' file? (???ゅ????'#'?風??琛?)
??? ????锛????ゅ????瀛?姣?xx??琛?
??? sed -e '/xx/d' file
??? ????: ???ら?ゅ????瀛?绗?覆xx??????琛?
??? sed -e '/xx/!d' file
(4) sed -e '/word1/, /word2/d' file? (???や???????璇?word1?闆??????璇?word2??琛?)
??? sed -e '10,/word1/d' file
??? ???ゆ??浠朵腑浠?绗?10琛??闆????word1??琛?
??? sed -e '/word1/,10/d' file
??? ??涓??㈢???歸???稿??锛????や?????word1??琛??扮??10琛?
(5) sed -e '/t.*t/d' file???? (???ゅ????涓や釜t??琛?)
??? ????锛????ゅ??????瀹?姝e?ㄨ〃杈懼??歸????琛???
???ゆ??????绗?浜?琛?锛?濡?涓?
sed -i '2d' test_sed
缁???锛?
Every 2.0s: cat /mnt/sed/test_sed Sun Mar 31 09:40:04 2019
ABCDEFGHIJ
BCDEFGHIJA
CDEFGHIJAB
DEFGHIJABC
???ゆ??浠舵????涓?琛?锛?
sed -i '$d' test_sed
?缁???锛?
Every 2.0s: cat /mnt/sed/test_sed Sun Mar 31 09:42:09 2019
ABCDEFGHIJ
BCDEFGHIJA
CDEFGHIJAB
???ゆ??浠剁??浜?琛??版??灏炬????琛?锛?
sed -i '2,$d' test_sed
缁???锛?
Every 2.0s: cat /mnt/sed/test_sed Sun Mar 31 09:44:22 2019
ABCDEFGHIJ
???ゆ????????A??琛?锛?
sed -i '/A/'d test_sed
?缁???锛??
# ??浠跺凡缁?琚?娓?绌?
Every 2.0s: cat /mnt/sed/test_sed
print ??=================================================================================??
print ??=================================================================================??
???幫?p?戒護
?烘???煎?锛?
??? [address1,[address2]] p
??? (1) sed -e '/then/ p' filename? #???版????琛?骞堕??澶????闆????then ??琛?
??? (2) sed -n '/then/ p' filename? #?????闆????then??琛?
??? (3) sed -e '1,3 p' filename???? # ???版????琛?骞堕??澶?1-3琛?
??? (4) sed -n '1,3 p' filename???? # ????-3琛?
??? (5) sed -n '/if/,/fi/ p' filename #???闆??绗?f??fi涔??寸????瀹?
??? p?芥?頒負sed?????闆?芥?幫??ㄨ???瑕?娉ㄦ??-e ??-n ???扮???哄????涓???浣跨??n???般??
print ??=================================================================================??
print ??=================================================================================??
?
?挎???s?戒護
Sed ???挎?㈡??浠朵腑??瀛?涓層??璧???琛??????寵????恒???朵腑,琛ㄧず?挎?㈠??涓茬????浠や腑???芥?闆???頒負s;琛ㄧず?挎?㈣???琛?????璧?????gt;????浠や腑???芥?闆???頒負c??涓?杩版???典互涓????涓?渚?瀛?璇存????
*琛????挎??(1) sed -e '1c/#!/bin/more' file (??绗?涓?琛??挎?㈡??#!/bin/more)
??? ????: ??绗?n琛??挎?㈡??just do it
??? sed -e 'nc/just do it' file
(2) sed -e '1,10c/I can do it' file? (??1??0琛??挎?㈡??涓?琛?:I can do it)
??? ????: ?㈡??涓よ?(I can do it! Let's start)
??? sed -e '1,10c/I can do it!/nLet'"/'"'s start' file
*瀛?绗????挎??(3) sed -e 's/word1/& word2/' file (灏?姣?涓?琛???word1??璇??挎?㈡??s???版??澶?涓?涓や釜浣?缃????扮?哥???,?芥?闆????涓???涓や釜?規????绗???
??? & : 浠h〃pattern
??? /n : 浠h〃 pattern 涓?琚?绗? n 涓? /( ??/)(??????褰? A]) ????璧鋒?ョ??瀛?涓層??渚?濡?
??? sed -e 's/w1/& w2/' file? # w1???版?矽???w1 w2
??? sed -e? 's//(test/) /(my/) /(car/)/[/2 /3 /1]/' file?? #缁???: [my car test]
*flag ???頒婦渚?
??? sed -e 's/w1/& w2/g' file
??? g : 浠h〃?挎?㈡?????歸??椤圭??;杩???,??浠朵腑????瀛?绗?覆w1?戒?琚??挎?㈡??瀛?涓?1 w2
??? sed -e 's/w1/& w2/10' file
??? m(10) : ?挎?㈣???绗?m涓?绗?????瀛?涓? 璁頒?锛???琛?????绗?m涓??歸????瀛?涓???? sed -e 's/w1/& w2/p' file
??? p : ?挎?㈢??涓?涓???w1?歸????瀛?绗?覆涓?1 w2锛?骞惰??哄?版????杈???
??? sed -e 's/w1/& w2/w w2file' file
??? w filename : 璇ュ???頒?灏??挎?㈣?????瀹瑰???ュ?版??浠?2file骞惰??烘?挎?㈠?????翠釜??浠躲??娉ㄦ??w2file???????????挎?㈣???琛?????? sed 'e 's/w1/& w2/' file
??? 杩?????flag 涓虹┖, 杩??峰氨????灏?绗?涓?涓?w1?歸????瀛?绗?覆?挎?㈡??w1 w2?????㈢??涓?杩?琛??挎????
*浣?缃????闆??ㄤ婦渚?
??? sed -e '/machine/s/phi/beta/g' file
??? 灏???浠朵腑??"machine"瀛?涓茬??璧???琛?涓???"phi"瀛?涓??挎?㈡??涓?beta"瀛?涓???? sed -e '1,10 s/w1/& w2/g' file
??? ??1??0????w1瀛?绗?覆?挎?㈡??w1 w2瀛?绗?覆??
??? sed -e '1,/else/ s/w1/& w2/g' file
??? ??1?闆??绗?覆else????w1瀛?绗?覆?挎?㈡??w1 w2瀛?绗?覆??
?跺??浣?缃????扮??搴??ㄤ????㈢???稿????
?
?ㄦ?磋????村????test?挎??負mytest??濡???娌℃??g??璁幫???????姣?琛?绗?涓?涓??歸????test琚??挎?㈡??mytest??
sed -i 's/test/mytest/' test_sed
?
?缁???锛?
[[email?protected] sed]# sed 's/test/mytest/' test_sed
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.
this is a mytest and test.
?
??g??璁幫?
sed 's/test/mytest/g' test_sed
缁???锛?
[[email?protected] sed]# sed 's/test/mytest/g' test_sed
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.
this is a mytest and mytest.
?(-n)??椤瑰??p??蹇?涓?璧蜂嬌?ㄨ〃绀哄?????伴?d??????挎?㈢??琛???涔?灏辨??璇達?濡?????涓?琛?寮?澶寸??test琚??挎?㈡??mytest锛?灏辨???闆????
sed -n 's/^test/mytest/p' test_sed
?娴?璇???妗o?
Every 2.0s: cat /mnt/sed/test_sed Sun Mar 31 10:03:43 2019
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
&绗??瘋〃绀烘?挎?㈡?㈠??绗?覆涓?琚??懼?扮???ㄤ喚??????浠?his寮?澶寸??琛??戒?琚??挎?㈡??瀹???宸插??This锛?????thisThis锛?濡?涓?
[[email?protected] sed]# sed 's/^this/&This/' test_sed
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.
thisThis is a test and test.
an琚???璁頒負1锛?????and浼?琚??挎?㈡??anand锛?濡?涓?
[[email?protected] sed]# sed 's/\(an\)d/\1and/' test_sed
this is a test anand test.
this is a test anand test.
this is a test anand test.
this is a test anand test.
this is a test anand test.
this is a test anand test.
涓?璁轟?涔?瀛?绗??绱ц???s?戒護???借?璁や負???扮??????绗????浠ワ???#???ㄨ?????????绗??浠f?誇?榛?璁ょ????/??????绗???琛ㄧず??????10?挎?㈡??100???
sed 's#10#100#g' expmple_file
?????ㄦā??hat??test??纭?瀹??????村????琛??借????幫?濡?涓?
娴?璇???妗o?
Every 2.0s: cat /mnt/sed/test_sed Sun Mar 31 10:13:08 2019
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
that is not a test;
娴?璇?缁???锛?
[[email?protected] sed]# sed -n '/that/, /test/p' test_sed
that is not a test;
???頒?绗?浜?琛?寮?濮??扮??涓?涓?????浠?est寮?濮???琛?涔??寸??????琛?锛?濡?涓?
娴?璇???妗o?
1 this is a test and test.
2 this is a test and test.
3 this is a test and test.
4 this is a test and test.
5 that is a test and test.
6 this is a test and test.
7
8 that is not a test;
~
~
~
"/mnt/sed/test_sed" 8L, 171C
娴?璇?缁???锛?
[[email?protected] sed]# sed -n '5, /this/p' test_sed
that is a test and test.
this is a test and test.
???版?????ㄦā??hat??that??纭?瀹??????村????琛??锛?濡?涓?
[[email?protected] sed]# sed -n '/that/, /that/p' test_sed
that is a test and test.
this is a test and test.
that is not a test;
?
瀵逛?妯℃??hat??that涔??寸??琛?锛?姣?琛?????灏劇?ㄥ??绗?覆sed test?挎???濡?涓?
[[email?protected] sed]# sed '/that/, /that/s/$/sed test/' test_sed
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
that is a test and test.sed test
this is a test and test.sed test
sed test
that is not a test;sed test
?print ??========================================================================================??
print ??========================================================================================??
澶??圭?杈?锛?e?戒護
(-e)??椤瑰??璁稿?ㄥ??涓?琛????ц?澶??″?戒護??濡?渚?瀛???绀猴?绗?涓??″?戒護??????琛?锛?绗?浜??″?戒護??heck?挎??est???戒護???ц?椤哄?瀵圭?????褰卞????濡???涓や釜?戒護?芥???挎?㈠?戒護锛??d?绗?涓?涓??挎?㈠?戒護灏?褰卞??绗?浜?涓??挎?㈠?戒護??缁?????
娴?璇???妗o?
1 this is a test and test.
2 this is a test and test.
3 this is a test and test.
4 this is a test and test.
5 that is a test and test.
6 this is a test and test.
7
8 that is not a test;
?娴?璇?缁???锛?
[[email?protected] sed]# sed -e '1,3d' -e 's/test/check/' test_sed
this is a check and test.
that is a check and test.
this is a check and test.
that is not a check;
??g锛?
[[email?protected] sed]# sed -e '1,3d' -e 's/test/check/g' test_sed
this is a check and check.
that is a check and check.
this is a check and check.
that is not a check;
???ゅ????not??琛?锛?
娴?璇???妗o?
1 this is a test and test.
2 this is a test and test.
3 this is a test and test.
4 this is a test and test.
5 that is a test and test.
6 this is a test and test.
7
8 that is not a test;
?娴?璇?缁???锛?
[[email?protected] sed]# sed '/not/d' test_sed
this is a test and test.
this is a test and test.
this is a test and test.
this is a test and test.
that is a test and test.
this is a test and test.
print ??==========================================================================================???
print ??==========================================================================================???
浠???浠惰?誨?ワ??
file??????瀹矽?璇昏??ワ??劇ず?ㄤ?test?歸????琛??????濡????歸??澶?琛?锛???file????瀹瑰??劇ず?ㄦ?????歸??琛???涓??????
sed '/test/r file' example
娴?璇???妗?est_sed锛?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
娴?璇???妗?est_sed02锛?
1 age 18 is man;
2 age 19 is woman;
3 age 20 is woman;
娴?璇?缁???锛?
[[email?protected] sed]# sed '/xiaoming/r test_sed02' test_sed
xiaoming age is 18;
age 18 is man;
age 19 is woman;
age 20 is woman;
xiaohong age is 19;
xiaohua age is 20;
娴?璇?缁???02锛?
[[email?protected] sed]# sed '/age/r test_sed02' test_sed
xiaoming age is 18;
age 18 is man;
age 19 is woman;
age 20 is woman;
xiaohong age is 19;
age 18 is man;
age 19 is woman;
age 20 is woman;
xiaohua age is 20;
age 18 is man;
age 19 is woman;
age 20 is woman;
print ??==========================================================================================???
print ??==========================================================================================???
???ユ??浠訛?w?戒護
??xample涓?????????test??琛??借?????ile????
sed -n '/test/w file' example
娴?璇???妗?est_sed锛?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
娴?璇???妗?est_sed02锛?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
?ц?锛?
sed -i '/age/w test_sed02' test_sed
娴?璇?缁???锛?test_sed02锛?锛?涓?杩版?ц?璇??ュ?test_sed涓?????age??琛????ヤ?test_sed02锛?test_sed02涓?????瀹矽?瑕???锛?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
print ??======================================================================================???
print ??======================================================================================???
杩藉???戒護锛?a?戒護
??This is a example??琚?杩藉???頒互test寮?澶寸??琛??????sed瑕?姹??戒護a???㈡??涓?涓?????????
sed '/^test/a\\This is a example' example
?娴?璇???妗o?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
娴?璇?缁???锛?
[[email?protected] sed]# sed '/^xiaoming/a\\This is a example' test_sed
xiaoming age is 18;
This is a example
xiaohong age is 19;
xiaohua age is 20;
print ??======================================================================================???
print ??======================================================================================???
???ワ?i?戒護
?烘???煎?:
??? [address] i/ ???ュ??瀹?filename
?word2)
璇存??:
?芥?闆????s 琛ㄧず?挎??substitute)??浠跺??瀛?涓層???舵??浠ゆ?煎?濡?涓? :
[address1[ ,address2]] s/pattern/replacemen/[flag]
??? sed -e '/#/i/words' file????? #??瀛?绗??????㈡???ヤ?琛?words
璇存??锛?
??? 杩??????芥?闆???版??i锛?瀹????芥??涓?涓??闆?????般??
??? sed -e '1/i/words' file
??? ?ㄧ??涓?琛?????涓?琛?words
??? cat "word" | sed -e '/$/.doc/g'?? #杈???ord.doc
??? ??ord???㈠??涓???缂???锛?浠???杈???ord.doc
??? i ???版?eソ涓?a???扮?稿??锛?瀹??????ュ?版??缁???瀹圭??????
a
??? a???扮??浣跨?ㄦ?煎?濡?涓?锛?
??? [address] a/ <???ュ??瀹?gt; filename
??? sed -e '/unix/a/ haha' test.txt?? #?ㄥ????unix??琛???娣誨??"haha"
??? #杈??虹???涓?
??????? unix
??????? haha
??? ???: sed -e '1 a/ hh' test.txt? #?ㄧ??涓?琛???娣誨??hh瀛?绗?
濡???test琚??歸??锛??????????????㈢?????????ュ?闆?歸??琛?????????
sed '/test/i\\new line-------------------------' example
娴?璇???妗o?test_sed
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
?娴?璇?缁???锛?
[[email?protected] sed]# sed '/^xiaoming/i\\--->this is a example' test_sed
--->this is a example
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 20;
?
print ??======================================================================================???
print ??======================================================================================???
璇誨??璧???涓?涓?琛?锛?n?戒護
?烘???煎?锛?
????[address1[ ,address2]] n
????sed -n -e '/echo/n' -e 'p' temp
????琛ㄧず杈??烘??浠訛?浣?濡???涓?琛?????瀛?绗?覆echo锛???杈??哄????璇ュ??绗?覆??涓?涓?琛???
????sed -n -e 'n' -e 'p' filename
????杈??烘??涓????舵?拌?
濡???test琚??歸??锛???绉誨?ㄥ?闆?歸??琛???涓?涓?琛?锛??挎?㈣?涓?琛???aa锛???涓?b锛?骞舵???拌?ヨ?锛??跺??缁х畫??
sed '/test/{ n; s/aa/bb/; }' example
娴?璇???妗o?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
娴?璇?缁???锛?
[[email?protected] sed]# sed '/xiaohong/{n; s/20/18/;}' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 18;
?
print ??======================================================================================???
print ??======================================================================================???
??褰??y?戒護
??1??10琛???????abcde杞???涓哄ぇ??锛?娉ㄦ??锛?姝e??琛ㄨ揪寮???瀛?绗???戒嬌?ㄨ?涓??戒護??
sed '1,10y/abcde/ABCDE/' example
娴?璇???妗o?test_sed
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
娴?璇?缁???锛?灏?1-2琛?涓???xmh?挎??負澶у??XMH
[[email?protected] sed]# sed '1,2y/xmh/XMH/' test_sed
XiaoMing age is 18;
XiaoHong age is 19;
xiaohua age is 20;
?娴?璇?缁???02锛?娉ㄦ??灏?????r?ㄦ?璇???妗d腑1-2琛?涓?瀛???
[[email?protected] sed]# sed '1,2y/xmhr/XMHR/' test_sed
XiaoMing age is 18;
XiaoHong age is 19;
xiaohua age is 20;
?
print ??======================================================================================???
print ??======================================================================================???
???猴?q?戒護
???闆????n琛???锛?????ed??
$ sed 'nq' example
娴?璇???妗o?test_sed
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
?娴?璇?缁???锛?
[[email?protected] sed]# sed '1q' test_sed
xiaoming age is 18;
[[email?protected] sed]# sed '2q' test_sed
xiaoming age is 18;
xiaohong age is 19;
[[email?protected] sed]# sed '3q' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 20;
?
print ??======================================================================================???
print ??======================================================================================???
淇??????峰??锛?h?戒護??G?戒護
??ed澶?????浠剁???跺??锛?姣?涓?琛??借?淇?瀛??ㄤ?涓???妯″?绌洪?寸??涓存?剁??插?轟腑锛??ら??琛?琚????ゆ????杈??鴻???娑?锛?????????琚?澶?????琛??藉????闆?ㄥ?骞?涓????ョ??妯″?绌洪?磋?娓?绌猴?骞跺???ユ?扮??涓?琛?绛?寰?澶??????ㄨ?涓?渚?瀛???锛??歸??test??琛?琚??懼?闆??锛?灏?瀛??ユā寮?绌洪?達?h?戒護灏??跺??跺苟瀛??ヤ?涓?绉頒負淇???缂?瀛??虹???規??缂??插?哄????绗?浜??¤???ョ????????锛?褰??拌揪????涓?琛???锛?G?戒護???轟???缂??插?虹??琛?锛??跺????瀹??懼??妯″?绌洪?翠腑锛?涓?杩藉???扮?闆?ㄥ凡缁?瀛??ㄤ?妯″?绌洪?翠腑??琛?????灏俱???ㄨ?涓?渚?瀛?涓?灏辨??杩藉???版????涓?琛???绠????ヨ?達?浠諱?????test??琛??借?澶??跺苟杩藉???拌?ユ??浠剁????灏俱??
sed -e '/test/h' -e '$G' example
娴?璇???妗o?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
?娴?璇?缁???锛?
# 灏?test_sed涓??歸????iaoming??琛?杩藉???版????涓?琛?
[[email?protected] sed]# sed -e '/xiaoming/h' -e '$G' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaohua age is 20;
xiaoming age is 18;
# 灏?test_sed涓??歸????iaoming??琛?杩藉???扮??2琛???????[[email?protected] sed]# sed -e '/xiaoming/h' -e '2G' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaoming age is 18;
xiaohua age is 20;
?
print ??======================================================================================???
print ??======================================================================================???
淇?????浜????h?戒護??x?戒護
浜??㈡ā寮?绌洪?村??淇???缂??插?虹????瀹廣??涔?灏辨????????test涓?check??琛?浜?????
sed -e '/test/h' -e '/check/x' example
娴?璇???妗o?test_sed
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
?娴?璇?缁???锛?锛?娉ㄦ??锛??歸???闆????xiaoming??琛?灏?????xiaohua??琛?缁??挎?㈡??浜?锛?
[[email?protected] sed]# sed -e '/xiaoming/h' -e '/xiaohua/x' test_sed
xiaoming age is 18;
xiaohong age is 19;
xiaoming age is 18;
?
print ??======================================================================================???
print ??======================================================================================???
????告?ц??戒護 : !
?????烘???煎?:
????[address1[ , address2]] ! ?芥?闆????
????sed -e '/18/!d' filename
???????ら?や?涓?????18??????琛???
娴?璇???妗o?test_sed
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
娴?璇?缁???锛?
# ???ゅ????18??????琛?
[[email?protected] sed]# sed -e '/18/d' test_sed
xiaohong age is 19;
xiaohua age is 20;
# ???や?????18??????琛?
[[email?protected] sed]# sed -e '/18/!d' test_sed
xiaoming age is 18;
print ??======================================================================================???
print ??======================================================================================???
?瑰????浠朵腑??璧???: c
?????烘???煎?锛?
????[address1[ ,address2]]c/ filename
?????芥?闆????c 绱ф?ョ?? "/" 瀛????ㄦ?ヨ〃绀烘?よ?缁??? , 浣跨?ㄨ????杈??ョ??璧???蹇?椤諱?涓?涓?琛?杈??ャ??濡???璧???瓒?杩?涓?琛? , ??椤誨??gt;姣?琛???缁?灏懼????/"
????sed -e '/zhengxh/c hhhh' filename
????琛ㄧず??????瀛?绗?覆zhengxh??琛?锛?璇ユ??hhhh??
娴?璇???妗o?
1 xiaoming age is 18;
2 xiaohong age is 19;
3 xiaohua age is 20;
娴?璇?缁???锛?
?
# 灏?????灏?????琛?锛??磋??挎??負mingming
[[email?protected] sed]# sed '/xiaoming/c mingming' test_sed
mingming
xiaohong age is 19;
xiaohua age is 20;
?
print ??======================================================================================???
print ??======================================================================================???
?
?