一、sed介紹
sed全稱(stream editor)流式編輯器,Sed主要用來自動編輯一個或多個文件、簡化對文件的反復(fù)操作、編寫轉(zhuǎn)換程序等,工作流程如下
1、sed概述
>sed?是一種在線的、非交互式的編輯器,它一次處理一行內(nèi)容,它是文本處理中非常好的工具,能夠完美的配合正則表達(dá)式使用,功能不同凡響。處理時,把當(dāng)前處理的行存儲在臨時緩沖區(qū)中,稱為“模式空間”(pattern?space),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有改變,除非你使用重定向存儲輸出,或者使用sed?-i選項 #-i選項就是將本該輸出到屏幕上的內(nèi)容輸出/流入文件中。Sed主要用來自動編輯一個或多個文件,可以將數(shù)據(jù)行進(jìn)行替換、刪除、新增、選取等特定工作,簡化對文件的反復(fù)操作,編寫轉(zhuǎn)換程序等
2、sed格式
#sed的命令格式:? ??sed?[options]?'command'?file(s); #sed的腳本格式:? ??sed?[options]?-f?scriptfile?file(s); #?注: ????sed和grep不一樣,不管是否找到指定的模式,它的退出狀態(tài)都是0 只有當(dāng)命令存在語法錯誤時,sed的退出狀態(tài)才不是0
二、sed選項與基本用法
1、sed選項
選項????????????????功能 -e:????????????????#允許多項編輯 -n:????????????????#取消默認(rèn)的輸出(模式空間的內(nèi)容輸出),只打印模式匹配的行; -i:????????????????#inplace,就地編輯,直接修改文件內(nèi)容; -r:????????????????#支持?jǐn)U展元字符 -f:????????????????#指定sed腳本文件名 -h或--help:?????#顯示幫助; -V或--version:??#顯示版本信息 #示例 ?sed?-r?''?/etc/passwd ?sed?-r?'p'?/etc/passwd ?sed?-r?-n?'p'?/etc/passwd???? #文件的一行行內(nèi)容相當(dāng)與水流,連續(xù)兩個-e就是設(shè)置了兩道關(guān)卡 [root@aliyun?~]#?sed?''?test.txt? 1111111 2222222egon 333333egon 444444egon 555555eon [root@aliyun?~]#?sed?-e?'3d'?-e?'1d'?test.txt?? 2222222egon 444444egon 555555eon [root@aliyun?~]#?sed?-rn?-e?'1,3d'?-e?'p'?test.txt 444444egon 555555eon [root@aliyun?~]#? #也可以將多道關(guān)卡寫入一個文件中 [root@aliyun?~]#?cat?sed.txt? 1,3d p [root@aliyun?~]#?sed?-rn?-f?sed.txt?test.txt 444444egon 555555eon [root@aliyun?~]#
2、sed命令組成
命令由”地址+命令“兩部分組成,命令如p、d,更多詳解第三章節(jié),本節(jié)我們主要介紹地址
地址用于決定對流入模式空間的哪些行進(jìn)行編輯,如果沒有指定地址,sed將處理流入模式空間的所有行。
1)數(shù)字
sed?-n?'p'?/etc/passwd???? sed?-n?'1,3p'?/etc/passwd???? sed?'1,47d'?/etc/passwd
2)正則表達(dá)式
與grep一樣,sed在文件中查找模式時也可以使用正則表達(dá)式(RE)和各種元字符。正則表達(dá)式是 括在斜杠間的模式,用于查找和替換,以下是sed支持的元字符。 #?使用基本元字符集?? ^,?$,?.,?*,?[],?[^],?\< \>,\(\),\{\} #?使用擴(kuò)展元字符集?? ?,?+,?{?},?|,?(?) #?使用擴(kuò)展元字符的方式: 轉(zhuǎn)義,如\+ -r參數(shù),如sed?-r [root@aliyun?~]#?cat?test.txt? 1111111 2222222egon 333333egon 444444egon 555555eon [root@aliyun?~]#?sed?-rn?'/egon/p'?test.txt? 2222222egon 333333egon 444444egon [root@aliyun?~]#
3)數(shù)字+正則表達(dá)式
[root@aliyun?~]#?cat?test.txt? 1111111 2222222egon 333333egon 444444egon 555555eon [root@aliyun?~]#?sed?-rn?'1,/egon/p'?test.txt? 1111111 2222222egon [root@aliyun?~]#? 解釋: #?"1,8p"代表打印1到8行,"1,/egon/p"則代表取從第1行到首次匹配到/egon/的行
3、cregexpc
地址可以是正則表達(dá)式,而正則表達(dá)式需要放置在\c與c中間,其中c可以是任意字符,但必須要加\轉(zhuǎn)義
[root@aliyun?~]#?cat?test.txt? 1111111 2222222egon 333333egon 444444egon 555555eon [root@aliyun?~]#?sed?-rn?'#egon#p'?test.txt? [root@aliyun?~]#?sed?-rn?'\#egon#p'?test.txt? 2222222egon 333333egon 444444egon [root@aliyun?~]#
如果c是左斜杠,不需要轉(zhuǎn)義也可以
[root@aliyun?~]#?sed?-rn?'\/egon/p'?test.txt? 2222222egon 333333egon 444444egon [root@aliyun?~]#?sed?-rn?'/egon/p'?test.txt? 2222222egon 333333egon 444444egon [root@aliyun?~]#
如果匹配的正則里有左斜杠,要么將正則轉(zhuǎn)義,要么將c轉(zhuǎn)義
[root@aliyun?~]#?cat?a.txt? /etc/egon/666 etc [root@aliyun?~]#?sed?-rn?'//etc/egon/666/p'?a.txt?#?錯誤 sed:?-e?expression?#1,?char?0:?no?previous?regular?expression ???? [root@aliyun?~]#?sed?-rn?'/\/etc\/egon\/666/p'?a.txt??#?正則轉(zhuǎn)義 /etc/egon/666 [root@aliyun?~]#?sed?-rn?'#/etc/egon/666#p'?a.txt?#?轉(zhuǎn)義c,必須是\c [root@aliyun?~]#?sed?-rn?'\#/etc/egon/666#p'?a.txt??#?轉(zhuǎn)義c /etc/egon/666 [root@aliyun?~]#? #?示例 [root@aliyun?~]#?cat?a.txt? /etc/egon/666 etc [root@aliyun?~]#?sed?-ri?'/\/etc\/egon\/666/s/.*/xxx/'?a.txt? [root@aliyun?~]#?cat?a.txt? xxx etc [root@aliyun?~]#
三、sed常用命令
sed命令告訴sed對指定行進(jìn)行何種操作,包括打印、刪除、修改等。
1、sed常用的參數(shù)
命令???????????????功能 a????????????????在當(dāng)前行后添加一行或多行 c????????????????用新文本修改(替換)當(dāng)前行中的文本 d????????????????刪除行 i????????????????在當(dāng)前行之前插入文本 l????????????????會用$符號標(biāo)識出文件中看不到的字符的位置 p????????????????打印行 n????????????????把下一行內(nèi)容讀入模式空間,后續(xù)的處理命令處理的都是剛讀入的新內(nèi)容 q????????????????結(jié)束或退出sed,不會將后續(xù)內(nèi)容讀入模式空間 r????????????????從文件中讀 !????????????????對所選行以外的所有行應(yīng)用命令 s????????????????用一個字符串替換另一個 w????????????????將行寫入文件 y????????????????將字符轉(zhuǎn)換為另一字符(不支持正則表達(dá)式),y/egon/1234/??e->1?g->2?o->3?n->4 h????????????????把模式空間里的內(nèi)容復(fù)制到暫存緩沖區(qū)(覆蓋) H????????????????把模式空間里的內(nèi)容追加到暫存緩沖區(qū) g????????????????取出暫存緩沖區(qū)的內(nèi)容,將其復(fù)制到模式空間,覆蓋該處原有內(nèi)容 G????????????????取出暫存緩沖區(qū)的內(nèi)容,將其復(fù)制到模式空間,追加在原有內(nèi)容后面 N???????????????追加下一個輸入行到模板塊后面并在二者間嵌入一個新行,改變當(dāng)前行號碼; x????????????????交換暫存緩沖區(qū)與模式空間的內(nèi)容 替換標(biāo)志?s g????????????????在行內(nèi)進(jìn)行全局替換 i????????????????忽略大小寫 ########################sed基礎(chǔ)命令###################### a\?在當(dāng)前行下面插入文本; ?i\?在當(dāng)前行上面插入文本; ?c\?把選定的行改為新的文本; ?d?刪除,刪除選擇的行; ?D?刪除模板塊的第一行; ?s?替換指定字符; ?h?拷貝模板塊的內(nèi)容到內(nèi)存中的緩沖區(qū); ?H?追加模板塊的內(nèi)容到內(nèi)存中的緩沖區(qū); ?g?獲得內(nèi)存緩沖區(qū)的內(nèi)容,并替代當(dāng)前模板塊中的文本; ?G?獲得內(nèi)存緩沖區(qū)的內(nèi)容,并追加到當(dāng)前模板塊文本的后面; ?l?列表不能打印字符的清單; ?n?讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令; ?N?追加下一個輸入行到模板塊后面并在二者間嵌入一個新行,改變當(dāng)前行號碼; ?p?打印模板塊的行。?P(大寫)?打印模板塊的第一行; ?q?退出Sed; ?b?lable?分支到腳本中帶有標(biāo)記的地方,如果分支不存在則分支到腳本的末尾; ?r?file?從file中讀行; ?t?label?if分支,從最后一行開始,條件一旦滿足或者T,t命令,將導(dǎo)致分支到帶有標(biāo)號的命令處,或者到腳本的末尾; ?T?label?錯誤分支,從最后一行開始,一旦發(fā)生錯誤或者T,t命令,將導(dǎo)致分支到帶有標(biāo)號的命令處,或者到腳本的末尾; ?w?file?寫并追加模板塊到file末尾; ?W?file?寫并追加模板塊的第一行到file末尾; ?!?表示后面的命令對所有沒有被選定的行發(fā)生作用; ?=?打印當(dāng)前行號; ?#?把注釋擴(kuò)展到下一個換行符以前; ########################sed替換標(biāo)記####################### ?g?表示行內(nèi)全面替換; ?p?表示打印行; ?w?表示把行寫入一個文件; ?x?表示互換模板塊中的文本和緩沖區(qū)中的文本; ?y?表示把一個字符翻譯為另外的字符(但是不用于正則表達(dá)式); ? ?\1?子串匹配標(biāo)記; ?&?已匹配字符串標(biāo)記; ######################sed元字符集####################### ^???匹配行開始,如:/^sed/匹配所有以sed開頭的行; ? $???匹配行結(jié)束,如:/sed$/匹配所有以sed結(jié)尾的行; ? ?.??匹配一個非換行符的任意字符,如:/s.d/匹配s后接一個任意字符,最后是d; ? ?*??匹配0個或多個字符,如:/*sed/匹配所有模板是一個或多個空格后緊跟sed的行; ?? ?[]?匹配一個指定范圍內(nèi)的字符,如/[ss]ed/匹配sed和Sed; ??? ?[^]?匹配一個不在指定范圍內(nèi)的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一個字母開頭,緊跟ed的行; ?? ?\(..\)匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替換成lovers; ?? ?&???保存搜索字符用來替換其他字符,如s/love/**&**/,love這成**love**; ?? ?\< 匹配單詞的開始,如:/\ \>??匹配單詞的結(jié)束,如/love\>/匹配包含以love結(jié)尾的單詞的行; ? ?x\{m\}???重復(fù)字符x,m次,如:/0\{5\}/匹配包含5個0的行; ? ?x\{m,\}??重復(fù)字符x,至少m次,如:/0\{5,\}/匹配至少有5個0的行; ? ?x\{m,n\}?重復(fù)字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10個0的行;
2、sed的實列
替換操作:s命令 替換文本中的字符串: ?sed?'s/book/books/'?file -n選項和p命令一起使用表示只打印那些發(fā)生替換的行: ?sed?-n?'s/test/TEST/p'?file 直接編輯文件選項-i,會匹配file文件中每一行的第一個book替換為books ?sed?-i?'s/book/books/g'?file 全面替換標(biāo)記g 使用后綴?/g?標(biāo)記會替換每一行中的所有匹配: ?sed?'s/book/books/g'?file 當(dāng)需要從第N處匹配開始替換時,可以使用?/Ng: ?echo?sksksksksksk?|?sed?'s/sk/SK/2g'? ?skSKSKSKSKSK ?echo?sksksksksksk?|?sed?'s/sk/SK/3g' ?skskSKSKSKSK?? ?echo?sksksksksksk?|?sed?'s/sk/SK/4g' ?skskskSKSKSK? 定界符 以上命令中字符?/?在sed中作為定界符使用,也可以使用任意的定界符 ?sed?'s:test:TEXT:g'? ?sed?'s|test|TEXT|g'? 定界符出現(xiàn)在樣式內(nèi)部時,需要進(jìn)行轉(zhuǎn)義: ?sed?'s/\/bin/\/usr\/local\/bin/g' 刪除操作:d命令 刪除空白行: ?sed?'/^$/d'?file 刪除文件的第2行: ?sed?'2d'?file 刪除文件的第2行到末尾所有行: ?sed?'2,$d'?file 刪除文件最后一行: ?sed?'$d'?file 刪除文件中所有開頭是test的行: ?sed?'/^test/'d?file 已匹配字符串標(biāo)記& 正則表達(dá)式?\w\+?匹配每一個單詞,使用?[&]?替換它,&?對應(yīng)于之前所匹配到的單詞: ?echo?this?is?a?test?line?|?sed?'s/\w\+/[&]/g' ?[this]?[is]?[a]?[test]?[line]? 所有以192.168.0.1開頭的行都會被替換成它自已加localhost: ?sed?'s/^192.168.0.1/&localhost/'?file?192.168.0.1localhost 子串匹配標(biāo)記\1 匹配給定樣式的其中一部分: ?echo?this?is?digit?7?in?a?number?|?sed?'s/digit?\([0-9]\)/\1/'? ?this?is?7?in?a?number 命令中?digit?7,被替換成了?7。樣式匹配到的子串是?7,\(..\)?用于匹配子串,對于匹配到的第一個子串就標(biāo)記為?\1,依此類推匹配到的第二個結(jié)果就是?\2,例如: ?echo?aaa?BBB?|?sed?'s/\([a-z]\+\)?\([A-Z]\+\)/\2?\1/'? ?BBB?aaa love被標(biāo)記為1,所有l(wèi)oveable會被替換成lovers,并打印出來: ?sed?-n?'s/\(love\)able/\1rs/p'?file 組合多個表達(dá)式 ?sed?'表達(dá)式'?|?sed?'表達(dá)式'??等價于:?? ?sed?'表達(dá)式;?表達(dá)式' 引用 sed表達(dá)式可以使用單引號來引用,但是如果表達(dá)式內(nèi)部包含變量字符串,就需要使用雙引號。 ?test=hello? ?echo?hello?WORLD?|?sed?"s/$test/HELLO"? ?HELLO?WORLD 選定行的范圍:,(逗號) 所有在模板test和check所確定的范圍內(nèi)的行都被打?。? ?sed?-n?'/test/,/check/p'?file 打印從第5行開始到第一個包含以test開始的行之間的所有行: ?sed?-n?'5,/^test/p'?file 對于模板test和west之間的行,每行的末尾用字符串a(chǎn)aa?bbb替換: ?sed?'/test/,/west/s/$/aaa?bbb/'?file 多點編輯:e命令 -e選項允許在同一行里執(zhí)行多條命令: ?sed?-e?'1,5d'?-e?'s/test/check/'?file 上面sed表達(dá)式的第一條命令刪除1至5行,第二條命令用check替換test。命令的執(zhí)行順序?qū)Y(jié)果有影響。如果兩個命令都是替換命令,那么第一個替換命令將影響第二個替換命令的結(jié)果。 和?-e?等價的命令是?--expression: ?sed?--expression='s/test/check/'?--expression='/love/d'?file 從文件讀入:r命令 file里的內(nèi)容被讀進(jìn)來,顯示在與test匹配的行后面,如果匹配多行,則file的內(nèi)容將顯示在所有匹配行的下面: ?sed?'/test/r?file'?filename 寫入文件:w命令 ? 在example中所有包含test的行都被寫入file里: ?sed?-n?'/test/w?file'?example 追加(行下):a\命令 將?this?is?a?test?line?追加到?以test?開頭的行后面: ?sed?'/^test/a\this?is?a?test?line'?file 在?test.conf?文件第2行之后插入?this?is?a?test?line: ?sed?-i?'2a\this?is?a?test?line'?test.conf? 插入(行上): i\命令?將?this?is?a?test?line?追加到以test開頭的行前面: ?sed?'/^test/i\this?is?a?test?line'?file 在test.conf文件第5行之前插入this?is?a?test?line: ?sed?-i?'5i\this?is?a?test?line'?test.conf 下一個:n命令 如果test被匹配,則移動到匹配行的下一行,替換這一行的aa,變?yōu)閎b,并打印該行,然后繼續(xù): ?sed?'/test/{?n;?s/aa/bb/;?}'?file? 變形:y命令 把1~10行內(nèi)所有abcde轉(zhuǎn)變?yōu)榇髮懀⒁?,正則表達(dá)式元字符不能使用這個命令: ?sed?'1,10y/abcde/ABCDE/'?file 退出:q命令 打印完第10行后,退出sed?sed?'10q'?file?保持和獲?。篽命令和G命令?在sed處理文件的時候,每一行都被保存在一個叫模式空間的臨時緩沖區(qū)中,除非行被刪除或者輸出被取消,否則所有被處理的行都將打印在屏幕上。接著模式空間被清空,并存入新的一行等待處理。 ?sed?-e?'/test/h'?-e?'$G'?file? 在這個例子里,匹配test的行被找到后,將存入模式空間,h命令將其復(fù)制并存入一個稱為保持緩存區(qū)的特殊緩沖區(qū)內(nèi)。第二條語句的意思是,當(dāng)?shù)竭_(dá)最后一行后,G命令取出保持緩沖區(qū)的行,然后把它放回模式空間中,且追加到現(xiàn)在已經(jīng)存在于模式空間中的行的末尾。在這個例子中就是追加到最后一行。簡單來說,任何包含test的行都被復(fù)制并追加到該文件的末尾。 保持和互換:h命令和x命令 互換模式空間和保持緩沖區(qū)的內(nèi)容。也就是把包含test與check的行互換: ?sed?-e?'/test/h'?-e?'/check/x'?file? 腳本scriptfile sed腳本是一個sed的命令清單,啟動Sed時以-f選項引導(dǎo)腳本文件名。Sed對于腳本中輸入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如果在一行中有多個命令,要用分號分隔。以#開頭的行為注釋行,且不能跨行。 ?sed?[options]?-f?scriptfile?file(s) 打印奇數(shù)行或偶數(shù)行 方法1: ?sed?-n?'p;n'?test.txt??#奇數(shù)行?sed?-n?'n;p'?test.txt??#偶數(shù)行? 方法2: ?sed?-n?'1~2p'?test.txt??#奇數(shù)行?sed?-n?'2~2p'?test.txt??#偶數(shù)行? 打印匹配字符串的下一行 ?grep?-A?1?SCC?URFILE? ?sed?-n?'/SCC/{n;p}'?URFILE? ?awk?'/SCC/{getline;?print}'
3、sed命令示例
打印命令:p #?sed?-r?"/egon/p"?a.txt???????? #?sed?-r?-n?"/egon/p"?a.txt 刪除命令:d,注意用單引號 #?sed?-r?'3d'?a.txt #?sed?-r?'3,$d'?a.txt #?sed?-r?'$d'?a.txt #?sed?-r?'/egon/d'?a.txt???? #?sed?-r?'1,/egon/{/egon/d}'?a.txt??????????#?只刪除模式匹配成功的第一行 [root@hzl?~]#?cat?a.txt? Egon111111 egon222222 333Egon333 444444egon 5555555555 6666666666 egon777777 8888888888 [root@hzl?~]#? [root@hzl?~]#?sed?-r?'/egon/d'?a.txt??#?只刪除模式匹配成功的所有行 Egon111111 333Egon333 5555555555 6666666666 8888888888 [root@hzl?~]#?sed?-r?'1,/egon/{/egon/d}'?a.txt??#?只刪除模式匹配成功的第一行 Egon111111 333Egon333 444444egon 5555555555 6666666666 egon777777 8888888888 替換命令:s #?sed?-r?'s/egon/Bigegon/'?a.txt? #?sed?-r?'s/egon/Bigegon/g'?a.txt? #?sed?-r?'s/^egon/Bigegon/g'?a.txt #?sed?-r?-n?'s/root/egon/gip'?/etc/passwd #?sed?-r?'s/[0-9]$/&.change/'?a.txt?????#?&代表取到匹配成功的整行內(nèi)容 #?sed?-r?'s/^([a-zA-Z]+)([^[a-zA-Z]+)/\2\1/'?a.txt #?sed?-r?'s#egon#bigegon#g'?a.txt??????????? 多重編輯命令:e #?sed?-r?-e?'1,3d'?-e?'s/[Ee]gon/EGON/g'?a.txt??#?在前一個-e的基礎(chǔ)之上進(jìn)行第二個-e操作 #?sed?-r?'1,3d;s/[Ee]gon/EGON/g'?a.txt #?sed?-r?'3{s/[0-9]/x/g;s/[Ee]gon/EGON/g}'?a.txt??#?只處理第三行 #?sed?-r?'1,3{s/[0-9]/x/g;s/[Ee]gon/EGON/g}'?a.txt??#?處理1到3行 #?sed?-r?-n?'1p;p'?a.txt??#?;分隔依次運行,先針對第一行進(jìn)行p操作,再針對所有行進(jìn)行p操作 #?sed?-r?-n?'1{p;p}'?a.txt??#?只針對第一行,連續(xù)進(jìn)行兩次p操作 反向選擇! #?sed?-r?'3d'?a.txt #?sed?-r?'3!d'?a.txt 讀文件命令:r #?sed?-r?'/^Egon/r?b.txt'?a.txt??#?在匹配成功的行后添加文件b.txt的內(nèi)容 #?sed?-r?'/2/r?b.txt'?a.txt??#?在第2行后面添加文件b.txt的內(nèi)容 寫文件命令:w #?sed?-r?'/[Ee]gon/w?b.txt'?a.txt??#?將匹配成功的行寫入新文件b.txt?????? #?sed?-r?'3,$w?/root/new.txt'?a.txt?#?將第3行到最后一行寫入/root/new.txt 追加命令:a #?sed?-r?'2aXXXXXXXXXXXXXXXXXXXX'?a.txt??#?在第2行后添加一行 #?sed?-r?'2a1111111111111\???????????????#?可以用\續(xù)行 >?222222222222\ >?333333333333'?a.txt 插入命令:i #?sed?-r?'2i1111111111111'?/etc/hosts #?sed?-r?'2i111111111\ >?2222222222\ >?3333333333'?a.txt 修改命令:c #?sed?-r?'2c1111111111111'?a.txt #?sed?-r?'2c111111111111\ >?22222222222\ >?33333333333'?a.txt 把下一行內(nèi)容讀入模式空間:n #?sed?-r?'/^Egon/{n;s/[0-9]/x/g}'?a.txt??#?將匹配/^Egon/成功的行的下一行讀入模式空間進(jìn)行s處理 [root@aliyun?~]#?cat?a.txt? /etc/egon/666 etc [root@aliyun?~]#?sed?-r?'\#/etc/egon/666#n;c?1111'?a.txt? /etc/egon/666 1111 [root@aliyun?~]#? 轉(zhuǎn)換命令:y #?sed?-r?'1,3y/Eeo/12X/'?a.txt??#?1到3行進(jìn)行轉(zhuǎn)換?對應(yīng)規(guī)則:a->1?e->2?o->X 退出:q #?sed?-r?'5q'?a.txt????????????? #?sed?-r?'/[Ee]gon/{?s/[0-9]/X/;?q;?}'?a.txt??#?匹配成功/[Ee]gon/則執(zhí)行{}內(nèi)命令,q代表退出,即替換一次則退出,如果文件中多行符合規(guī)則的內(nèi)容也只替換了第一個
四、模式空間與保持空間
1、sed 有兩個內(nèi)置的存儲空間:
1)模式空間(pattern space):
如你所知,模式空間用于 sed 執(zhí)行的正常流程中。該空間 sed 內(nèi)置的一個緩沖區(qū),用來存放、修改從輸入文件讀取的內(nèi)容。
2)保持空間(hold space):
保持空間是另外一個緩沖區(qū),用來存放臨時數(shù)據(jù)。Sed 可以在保持空間和模式空間交換數(shù)據(jù),但是不能在保持空間上執(zhí)行普通的 sed 命令。
2、模式空間與保持空間的操作命令
x:命令x(exchange)?用于交換模式空間和保持空間的內(nèi)容 h:模式空間復(fù)制/覆蓋到保持空間 H:模式空間追加到保持空間 g:保持空間復(fù)制/覆蓋到模式空間 G:保持空間追加到模式空間 n:讀取下一行到/覆蓋到模式空間 N:將下一行添加到模式空間 d:刪除pattern?space中的所有行,并讀入下一新行到pattern?space中
3、示例(交換文件的行)
[root@hzl?~]#?cat?test.txt? 1111 2222 3333 #?======================方式1:====================== [root@hzl?~]#?tac?test.txt? 3333 2222 1111 [root@hzl?~]#? #?======================方式2:====================== 思路: #?1、讀取文件第一行內(nèi)容到模式空間,進(jìn)行的操作如下?? #?將模式空間內(nèi)容覆蓋到保持空間 #?刪除模式空間內(nèi)容 ??? #?2、讀取文件第二行內(nèi)容到模式空間,進(jìn)行的操作如下?? #?將保持內(nèi)容追加到模式空間 #?將模式空間內(nèi)容覆蓋到保持空間 #?刪除模式空間內(nèi)容? #?3、讀取文件第三行內(nèi)容到模式空間,進(jìn)行的操作如下?? #?將保持空間內(nèi)容追加到模式空間 實現(xiàn): sed?-r?'1h;1d;2G;2h;2d;3G'?test.txt 或者 sed?'1!G;h;$!d'?test.txt
五、sed腳本
sed腳本就是寫在文件中的一系列sed命令,使用-f 選項指定sed腳本文件名,需要注意的問題如下
腳本末尾不能有任何多余的空格或文本
如果命令不能獨占一行,就必須以\結(jié)尾
腳本中不能使用引號,除非它們是查找串的一部分
反斜杠起到續(xù)行的作用
[root@egon?~]#?cat?sed.sh?#永久存儲,存了多行sed命令,相當(dāng)于多道關(guān)卡,每讀入一行內(nèi)容將經(jīng)歷一道道關(guān)卡 1h;1d;2G;2h;2d;3G 1h;1d;2G;2h;2d;3G [root@egon?~]#?sed?-r?''?a.txt 1111 2222 3333 [root@egon?~]#? [root@egon?~]#?sed?-r?-f?sed.sh?test.txt? 3333 2222 1111 2222 1111 [root@egon?~]#
六、sed使用練習(xí)
#注釋的行 sed?-r?-i?'/^#/d'?file.conf? sed?-r?-i?'/^[?\t]*#/d'?file.conf 刪除配置文件中用雙斜杠//注釋的行? sed?-r?-i?'\c//cd'?file.conf 刪除無內(nèi)容空行? sed?-r?'/^$/d'?file.conf? sed?-r?'/^[\t]*$/d'?file.conf? sed?-r?'/^[?\t]*$/d'?file.conf 示例: #?刪除#號注釋和無內(nèi)容的空行 sed?-r?-i?'/^[?\t]*#/d;?/^[?\t]*$/d'?/etc/vsftpd/vsftpd.conf sed?-r?-i?'/^[?\t]*#|^[?\t]*$/d'?/etc/vsftpd/vsftpd.conf?#?同上 追加一行,\可有可無,有更清晰 sed?-r?-i?'$a\chroot_local_user=YES'?/etc/vsftpd/vsftpd.conf? 給文件每行加注釋 sed?-r?-i?'s/^/#/'?filename 每指定行加注釋 sed?-r?-i?'10,$s/^/#/'?filename sed?-r?'3,$s/^#*/#/'?filename????????#?將行首連續(xù)的零個或多個#換成一個# sed中使用外部變量 #?var1=666 #?sed?-r?3a$var1?test.txt????#?可以不加引號 #?sed?-r?"3a$var1"?test.txt??#?也可以加引號,但注意是雙引號而不是單引號,因為要用$符號取變量值 #?sed?-r?'3a'"$var1"?test.txt?#?也可以sed命令用''引起來,而變量用"",注意二者之間不能有空格
The above is the detailed content of How to use the sed command in linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. The Origin of .NETCore When talking about .NETCore, we must not mention its predecessor .NET. Java was in the limelight at that time, and Microsoft also favored Java. The Java virtual machine on the Windows platform was developed by Microsoft based on JVM standards. It is said to be the best performance Java virtual machine at that time. However, Microsoft has its own little abacus, trying to bundle Java with the Windows platform and add some Windows-specific features. Sun's dissatisfaction with this led to a breakdown of the relationship between the two parties, and Microsoft then launched .NET. .NET has borrowed many features of Java since its inception and gradually surpassed Java in language features and form development. Java in version 1.6

Integrating Postman applications on CentOS can be achieved through a variety of methods. The following are the detailed steps and suggestions: Install Postman by downloading the installation package to download Postman's Linux version installation package: Visit Postman's official website and select the version suitable for Linux to download. Unzip the installation package: Use the following command to unzip the installation package to the specified directory, for example /opt: sudotar-xzfpostman-linux-x64-xx.xx.xx.tar.gz-C/opt Please note that "postman-linux-x64-xx.xx.xx.tar.gz" is replaced by the file name you actually downloaded. Create symbols

The main difference between Java and other programming languages ??is its cross-platform feature of "writing at once, running everywhere". 1. The syntax of Java is close to C, but it removes pointer operations that are prone to errors, making it suitable for large enterprise applications. 2. Compared with Python, Java has more advantages in performance and large-scale data processing. The cross-platform advantage of Java stems from the Java virtual machine (JVM), which can run the same bytecode on different platforms, simplifying development and deployment, but be careful to avoid using platform-specific APIs to maintain cross-platformity.

Setting the location of the interpreter in PyCharm can be achieved through the following steps: 1. Open PyCharm, click the "File" menu, and select "Settings" or "Preferences". 2. Find and click "Project:[Your Project Name]" and select "PythonInterpreter". 3. Click "AddInterpreter", select "SystemInterpreter", browse to the Python installation directory, select the Python executable file, and click "OK". When setting up the interpreter, you need to pay attention to path correctness, version compatibility and the use of the virtual environment to ensure the smooth operation of the project.

The steps to manually install the plug-in package in VSCode are: 1. Download the .vsix file of the plug-in; 2. Open VSCode and press Ctrl Shift P (Windows/Linux) or Cmd Shift P (Mac) to call up the command panel; 3. Enter and select Extensions:InstallfromVSIX..., then select .vsix file and install. Manually installing plug-ins provides a flexible way to install, especially when the network is restricted or the plug-in market is unavailable, but attention needs to be paid to file security and possible dependencies.

[Common Directory Description] Directory/bin stores binary executable files (ls, cat, mkdir, etc.), and common commands are generally here. /etc stores system management and configuration files/home stores all user files. The root directory of the user's home directory is the basis of the user's home directory. For example, the home directory of the user user is /home/user. You can use ~user to represent /usr to store system applications. The more important directory /usr/local Local system administrator software installation directory (install system-level applications). This is the largest directory, and almost all the applications and files to be used are in this directory. /usr/x11r6?Directory for storing x?window/usr/bin?Many

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

The installation and configuration of MySQL can be completed through the following steps: 1. Download the installation package suitable for the operating system from the official website. 2. Run the installer, select the "Developer Default" option and set the root user password. 3. After installation, configure environment variables to ensure that the bin directory of MySQL is in PATH. 4. When creating a user, follow the principle of minimum permissions and set a strong password. 5. Adjust the innodb_buffer_pool_size and max_connections parameters when optimizing performance. 6. Back up the database regularly and optimize query statements to improve performance.
