亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
一、sed介紹
1、sed概述
2、sed格式
二、sed選項(xiàng)與基本用法
1、sed選項(xiàng)
2、sed命令組成
1)數(shù)字
2)正則表達(dá)式
3)數(shù)字+正則表達(dá)式
3、cregexpc
三、sed常用命令
1、sed常用的參數(shù)
2、sed的實(shí)列
3、sed命令示例
四、模式空間與保持空間
1、sed 有兩個(gè)內(nèi)置的存儲(chǔ)空間:
1)模式空間(pattern space):
2)保持空間(hold space):
2、模式空間與保持空間的操作命令
3、示例(交換文件的行)
五、sed腳本
六、sed使用練習(xí)
Home Operation and Maintenance Linux Operation and Maintenance How to use the sed command in linux

How to use the sed command in linux

May 18, 2023 pm 10:07 PM
linux sed

一、sed介紹

sed全稱(chēng)(stream editor)流式編輯器,Sed主要用來(lái)自動(dòng)編輯一個(gè)或多個(gè)文件、簡(jiǎn)化對(duì)文件的反復(fù)操作、編寫(xiě)轉(zhuǎn)換程序等,工作流程如下

1、sed概述

>sed?是一種在線(xiàn)的、非交互式的編輯器,它一次處理一行內(nèi)容,它是文本處理中非常好的工具,能夠完美的配合正則表達(dá)式使用,功能不同凡響。處理時(shí),把當(dāng)前處理的行存儲(chǔ)在臨時(shí)緩沖區(qū)中,稱(chēng)為“模式空間”(pattern?space),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒(méi)有改變,除非你使用重定向存儲(chǔ)輸出,或者使用sed?-i選項(xiàng)
#-i選項(xiàng)就是將本該輸出到屏幕上的內(nèi)容輸出/流入文件中。Sed主要用來(lái)自動(dòng)編輯一個(gè)或多個(gè)文件,可以將數(shù)據(jù)行進(jìn)行替換、刪除、新增、選取等特定工作,簡(jiǎn)化對(duì)文件的反復(fù)操作,編寫(xiě)轉(zhuǎn)換程序等

2、sed格式

#sed的命令格式:?
??sed?[options]?'command'?file(s);
#sed的腳本格式:?
??sed?[options]?-f?scriptfile?file(s);



#?注:
????sed和grep不一樣,不管是否找到指定的模式,它的退出狀態(tài)都是0
只有當(dāng)命令存在語(yǔ)法錯(cuò)誤時(shí),sed的退出狀態(tài)才不是0

二、sed選項(xiàng)與基本用法

1、sed選項(xiàng)

選項(xiàng)????????????????功能
-e:????????????????#允許多項(xiàng)編輯
-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ù)兩個(gè)-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)卡寫(xiě)入一個(gè)文件中
[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é)我們主要介紹地址

地址用于決定對(duì)流入模式空間的哪些行進(jìn)行編輯,如果沒(méi)有指定地址,sed將處理流入模式空間的所有行。

1)數(shù)字
sed?-n?'p'?/etc/passwd????
sed?-n?'1,3p'?/etc/passwd????
sed?'1,47d'?/etc/passwd
2)正則表達(dá)式
與grep一樣,sed在文件中查找模式時(shí)也可以使用正則表達(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?~]#?

解釋?zhuān)?#?"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?#?錯(cuò)誤
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對(duì)指定行進(jìn)行何種操作,包括打印、刪除、修改等。

1、sed常用的參數(shù)

命令???????????????功能
a????????????????在當(dāng)前行后添加一行或多行
c????????????????用新文本修改(替換)當(dāng)前行中的文本
d????????????????刪除行
i????????????????在當(dāng)前行之前插入文本
l????????????????會(huì)用$符號(hào)標(biāo)識(shí)出文件中看不到的字符的位置
p????????????????打印行
n????????????????把下一行內(nèi)容讀入模式空間,后續(xù)的處理命令處理的都是剛讀入的新內(nèi)容
q????????????????結(jié)束或退出sed,不會(huì)將后續(xù)內(nèi)容讀入模式空間
r????????????????從文件中讀
!????????????????對(duì)所選行以外的所有行應(yīng)用命令
s????????????????用一個(gè)字符串替換另一個(gè)
w????????????????將行寫(xiě)入文件
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???????????????追加下一個(gè)輸入行到模板塊后面并在二者間嵌入一個(gè)新行,改變當(dāng)前行號(hào)碼;

x????????????????交換暫存緩沖區(qū)與模式空間的內(nèi)容

替換標(biāo)志?s
g????????????????在行內(nèi)進(jìn)行全局替換
i????????????????忽略大小寫(xiě)



########################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?讀取下一個(gè)輸入行,用下一個(gè)命令處理新的行而不是用第一個(gè)命令;

?N?追加下一個(gè)輸入行到模板塊后面并在二者間嵌入一個(gè)新行,改變當(dāng)前行號(hào)碼;

?p?打印模板塊的行。?P(大寫(xiě))?打印模板塊的第一行;

?q?退出Sed;

?b?lable?分支到腳本中帶有標(biāo)記的地方,如果分支不存在則分支到腳本的末尾;

?r?file?從file中讀行;

?t?label?if分支,從最后一行開(kāi)始,條件一旦滿(mǎn)足或者T,t命令,將導(dǎo)致分支到帶有標(biāo)號(hào)的命令處,或者到腳本的末尾;

?T?label?錯(cuò)誤分支,從最后一行開(kāi)始,一旦發(fā)生錯(cuò)誤或者T,t命令,將導(dǎo)致分支到帶有標(biāo)號(hào)的命令處,或者到腳本的末尾;

?w?file?寫(xiě)并追加模板塊到file末尾;

?W?file?寫(xiě)并追加模板塊的第一行到file末尾;

?!?表示后面的命令對(duì)所有沒(méi)有被選定的行發(fā)生作用;

?=?打印當(dāng)前行號(hào);

?#?把注釋擴(kuò)展到下一個(gè)換行符以前;


########################sed替換標(biāo)記#######################
?g?表示行內(nèi)全面替換;

?p?表示打印行;

?w?表示把行寫(xiě)入一個(gè)文件;

?x?表示互換模板塊中的文本和緩沖區(qū)中的文本;

?y?表示把一個(gè)字符翻譯為另外的字符(但是不用于正則表達(dá)式);
?
?\1?子串匹配標(biāo)記;

?&?已匹配字符串標(biāo)記;





######################sed元字符集#######################
^???匹配行開(kāi)始,如:/^sed/匹配所有以sed開(kāi)頭的行;
?
$???匹配行結(jié)束,如:/sed$/匹配所有以sed結(jié)尾的行;
?
?.??匹配一個(gè)非換行符的任意字符,如:/s.d/匹配s后接一個(gè)任意字符,最后是d;
?
?*??匹配0個(gè)或多個(gè)字符,如:/*sed/匹配所有模板是一個(gè)或多個(gè)空格后緊跟sed的行;
??
?[]?匹配一個(gè)指定范圍內(nèi)的字符,如/[ss]ed/匹配sed和Sed;
???
?[^]?匹配一個(gè)不在指定范圍內(nèi)的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一個(gè)字母開(kāi)頭,緊跟ed的行;
??
?\(..\)匹配子串,保存匹配的字符,如s/\(love\)able/\1rs,loveable被替換成lovers;
??
?&???保存搜索字符用來(lái)替換其他字符,如s/love/**&**/,love這成**love**;
??
?\<  匹配單詞的開(kāi)始,如:/\ 
 \>??匹配單詞的結(jié)束,如/love\>/匹配包含以love結(jié)尾的單詞的行;
?
?x\{m\}???重復(fù)字符x,m次,如:/0\{5\}/匹配包含5個(gè)0的行;
?
?x\{m,\}??重復(fù)字符x,至少m次,如:/0\{5,\}/匹配至少有5個(gè)0的行;
?
?x\{m,n\}?重復(fù)字符x,至少m次,不多于n次,如:/0\{5,10\}/匹配5~10個(gè)0的行;
2、sed的實(shí)列
替換操作:s命令

替換文本中的字符串:

?sed?'s/book/books/'?file
-n選項(xiàng)和p命令一起使用表示只打印那些發(fā)生替換的行:

?sed?-n?'s/test/TEST/p'?file
直接編輯文件選項(xiàng)-i,會(huì)匹配file文件中每一行的第一個(gè)book替換為books

?sed?-i?'s/book/books/g'?file
全面替換標(biāo)記g

使用后綴?/g?標(biāo)記會(huì)替換每一行中的所有匹配:

?sed?'s/book/books/g'?file
當(dāng)需要從第N處匹配開(kāi)始替換時(shí),可以使用?/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)部時(shí),需要進(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
刪除文件中所有開(kāi)頭是test的行:

?sed?'/^test/'d?file
已匹配字符串標(biāo)記&

正則表達(dá)式?\w\+?匹配每一個(gè)單詞,使用?[&]?替換它,&?對(duì)應(yīng)于之前所匹配到的單詞:

?echo?this?is?a?test?line?|?sed?'s/\w\+/[&]/g'
?[this]?[is]?[a]?[test]?[line]?
所有以192.168.0.1開(kāi)頭的行都會(huì)被替換成它自已加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,\(..\)?用于匹配子串,對(duì)于匹配到的第一個(gè)子串就標(biāo)記為?\1,依此類(lèi)推匹配到的第二個(gè)結(jié)果就是?\2,例如:

?echo?aaa?BBB?|?sed?'s/\([a-z]\+\)?\([A-Z]\+\)/\2?\1/'?
?BBB?aaa
love被標(biāo)記為1,所有l(wèi)oveable會(huì)被替換成lovers,并打印出來(lái):

?sed?-n?'s/\(love\)able/\1rs/p'?file
組合多個(gè)表達(dá)式

?sed?'表達(dá)式'?|?sed?'表達(dá)式'??等價(jià)于:??
?sed?'表達(dá)式;?表達(dá)式'
引用

sed表達(dá)式可以使用單引號(hào)來(lái)引用,但是如果表達(dá)式內(nèi)部包含變量字符串,就需要使用雙引號(hào)。

?test=hello?
?echo?hello?WORLD?|?sed?"s/$test/HELLO"?
?HELLO?WORLD
選定行的范圍:,(逗號(hào))

所有在模板test和check所確定的范圍內(nèi)的行都被打?。?
?sed?-n?'/test/,/check/p'?file
打印從第5行開(kāi)始到第一個(gè)包含以test開(kāi)始的行之間的所有行:

?sed?-n?'5,/^test/p'?file
對(duì)于模板test和west之間的行,每行的末尾用字符串a(chǎn)aa?bbb替換:

?sed?'/test/,/west/s/$/aaa?bbb/'?file
多點(diǎn)編輯:e命令

-e選項(xiàng)允許在同一行里執(zhí)行多條命令:

?sed?-e?'1,5d'?-e?'s/test/check/'?file
上面sed表達(dá)式的第一條命令刪除1至5行,第二條命令用check替換test。命令的執(zhí)行順序?qū)Y(jié)果有影響。如果兩個(gè)命令都是替換命令,那么第一個(gè)替換命令將影響第二個(gè)替換命令的結(jié)果。

和?-e?等價(jià)的命令是?--expression:

?sed?--expression='s/test/check/'?--expression='/love/d'?file
從文件讀入:r命令

file里的內(nèi)容被讀進(jìn)來(lái),顯示在與test匹配的行后面,如果匹配多行,則file的內(nèi)容將顯示在所有匹配行的下面:

?sed?'/test/r?file'?filename
寫(xiě)入文件:w命令
?
在example中所有包含test的行都被寫(xiě)入file里:

?sed?-n?'/test/w?file'?example
追加(行下):a\命令

將?this?is?a?test?line?追加到?以test?開(kāi)頭的行后面:

?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開(kāi)頭的行前面:

?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
下一個(gè):n命令

如果test被匹配,則移動(dòng)到匹配行的下一行,替換這一行的aa,變?yōu)閎b,并打印該行,然后繼續(xù):

?sed?'/test/{?n;?s/aa/bb/;?}'?file?
變形:y命令

把1~10行內(nèi)所有abcde轉(zhuǎn)變?yōu)榇髮?xiě),注意,正則表達(dá)式元字符不能使用這個(gè)命令:

?sed?'1,10y/abcde/ABCDE/'?file
退出:q命令

打印完第10行后,退出sed?sed?'10q'?file?保持和獲?。篽命令和G命令?在sed處理文件的時(shí)候,每一行都被保存在一個(gè)叫模式空間的臨時(shí)緩沖區(qū)中,除非行被刪除或者輸出被取消,否則所有被處理的行都將打印在屏幕上。接著模式空間被清空,并存入新的一行等待處理。

?sed?-e?'/test/h'?-e?'$G'?file?
在這個(gè)例子里,匹配test的行被找到后,將存入模式空間,h命令將其復(fù)制并存入一個(gè)稱(chēng)為保持緩存區(qū)的特殊緩沖區(qū)內(nèi)。第二條語(yǔ)句的意思是,當(dāng)?shù)竭_(dá)最后一行后,G命令取出保持緩沖區(qū)的行,然后把它放回模式空間中,且追加到現(xiàn)在已經(jīng)存在于模式空間中的行的末尾。在這個(gè)例子中就是追加到最后一行。簡(jiǎn)單來(lái)說(shuō),任何包含test的行都被復(fù)制并追加到該文件的末尾。

保持和互換:h命令和x命令

互換模式空間和保持緩沖區(qū)的內(nèi)容。也就是把包含test與check的行互換:

?sed?-e?'/test/h'?-e?'/check/x'?file?
腳本scriptfile

sed腳本是一個(gè)sed的命令清單,啟動(dòng)Sed時(shí)以-f選項(xiàng)引導(dǎo)腳本文件名。Sed對(duì)于腳本中輸入的命令非常挑剔,在命令的末尾不能有任何空白或文本,如果在一行中有多個(gè)命令,要用分號(hào)分隔。以#開(kāi)頭的行為注釋行,且不能跨行。

?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,注意用單引號(hào)
#?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??#?在前一個(gè)-e的基礎(chǔ)之上進(jìn)行第二個(gè)-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??#?;分隔依次運(yùn)行,先針對(duì)第一行進(jìn)行p操作,再針對(duì)所有行進(jìn)行p操作
#?sed?-r?-n?'1{p;p}'?a.txt??#?只針對(duì)第一行,連續(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)容

寫(xiě)文件命令:w
#?sed?-r?'/[Ee]gon/w?b.txt'?a.txt??#?將匹配成功的行寫(xiě)入新文件b.txt??????
#?sed?-r?'3,$w?/root/new.txt'?a.txt?#?將第3行到最后一行寫(xiě)入/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)換?對(duì)應(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)容也只替換了第一個(gè)

四、模式空間與保持空間

1、sed 有兩個(gè)內(nèi)置的存儲(chǔ)空間:

1)模式空間(pattern space):

如你所知,模式空間用于 sed 執(zhí)行的正常流程中。該空間 sed 內(nèi)置的一個(gè)緩沖區(qū),用來(lái)存放、修改從輸入文件讀取的內(nèi)容。

2)保持空間(hold space):

保持空間是另外一個(gè)緩沖區(qū),用來(lái)存放臨時(shí)數(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)容追加到模式空間



實(shí)現(xiàn):
sed?-r?'1h;1d;2G;2h;2d;3G'?test.txt
或者
sed?'1!G;h;$!d'?test.txt

五、sed腳本

sed腳本就是寫(xiě)在文件中的一系列sed命令,使用-f 選項(xiàng)指定sed腳本文件名,需要注意的問(wèn)題如下

  • 腳本末尾不能有任何多余的空格或文本

  • 如果命令不能獨(dú)占一行,就必須以\結(jié)尾

  • 腳本中不能使用引號(hào),除非它們是查找串的一部分

  • 反斜杠起到續(xù)行的作用

[root@egon?~]#?cat?sed.sh?#永久存儲(chǔ),存了多行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

刪除無(wú)內(nèi)容空行?
sed?-r?'/^$/d'?file.conf?
sed?-r?'/^[\t]*$/d'?file.conf?
sed?-r?'/^[?\t]*$/d'?file.conf


示例:
#?刪除#號(hào)注釋和無(wú)內(nèi)容的空行
sed?-r?-i?'/^[?\t]*#/d;?/^[?\t]*$/d'?/etc/vsftpd/vsftpd.conf
sed?-r?-i?'/^[?\t]*#|^[?\t]*$/d'?/etc/vsftpd/vsftpd.conf?#?同上

追加一行,\可有可無(wú),有更清晰
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ù)的零個(gè)或多個(gè)#換成一個(gè)#


sed中使用外部變量
#?var1=666
#?sed?-r?3a$var1?test.txt????#?可以不加引號(hào)
#?sed?-r?"3a$var1"?test.txt??#?也可以加引號(hào),但注意是雙引號(hào)而不是單引號(hào),因?yàn)橐?符號(hào)取變量值
#?sed?-r?'3a'"$var1"?test.txt?#?也可以sed命令用''引起來(lái),而變量用"",注意二者之間不能有空格

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How does the cost of ownership differ between Linux and Windows? How does the cost of ownership differ between Linux and Windows? Jun 09, 2025 am 12:17 AM

Linux's cost of ownership is usually lower than Windows. 1) Linux does not require license fees, saving a lot of costs, while Windows requires purchasing a license. 2) Linux has low hardware requirements and can extend the service life of the device. 3) The Linux community provides free support to reduce maintenance costs. 4) Linux is highly secure and reduces productivity losses. 5) The Linux learning curve is steep, but Windows is easier to use. The choice should be based on specific needs and budget.

How does the performance of I/O operations differ between Linux and Windows? How does the performance of I/O operations differ between Linux and Windows? Jun 07, 2025 am 12:06 AM

LinuxoftenoutperformsWindowsinI/Operformanceduetoitscustomizablekernelandfilesystems,whileWindowsoffersmoreuniformperformanceacrosshardware.1)LinuxexcelswithcustomizableI/OschedulerslikeCFQandDeadline,enhancingperformanceinhigh-throughputapplications

How to install Linux alongside Windows (dual boot)? How to install Linux alongside Windows (dual boot)? Jun 18, 2025 am 12:19 AM

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

How to enable the EPEL (Extra Packages for Enterprise Linux) repository? How to enable the EPEL (Extra Packages for Enterprise Linux) repository? Jun 17, 2025 am 09:15 AM

The key to enabling EPEL repository is to select the correct installation method according to the system version. First, confirm the system type and version, and use the command cat/etc/os-release to obtain information; second, enable EPEL through dnfinstallepel-release on CentOS/RockyLinux, and the 8 and 9 version commands are the same; third, you need to manually download the corresponding version of the .repo file and install it on RHEL; fourth, you can re-import the GPG key when encountering problems. Note that the old version may not be supported, and you can also consider enabling epel-next to obtain the test package. After completing the above steps, use dnfrepolist to verify that the EPEL repository is successfully added.

How does Linux perform compared to Windows for web server workloads? How does Linux perform compared to Windows for web server workloads? Jun 08, 2025 am 12:18 AM

Linux usually performs better in web server performance, mainly due to its advantages in kernel optimization, resource management and open source ecosystem. 1) After years of optimization of the Linux kernel, mechanisms such as epoll and kqueue make it more efficient in handling high concurrent requests. 2) Linux provides fine-grained resource management tools such as cgroups. 3) The open source community continuously optimizes Linux performance, and many high-performance web servers such as Nginx are developed on Linux. By contrast, Windows performs well when handling ASP.NET applications and provides better development tools and commercial support.

How to choose a Linux distro for a beginner? How to choose a Linux distro for a beginner? Jun 19, 2025 am 12:09 AM

Newbie users should first clarify their usage requirements when choosing a Linux distribution. 1. Choose Ubuntu or LinuxMint for daily use; programming and development are suitable for Manjaro or Fedora; use Lubuntu and other lightweight systems for old devices; recommend CentOSStream or Debian to learn the underlying principles. 2. Stability is preferred for UbuntuLTS or Debian; you can choose Arch or Manjaro to pursue new features. 3. In terms of community support, Ubuntu and LinuxMint are rich in resources, and Arch documents are technically oriented. 4. In terms of installation difficulty, Ubuntu and LinuxMint are relatively simple, and Arch is suitable for those with basic needs. It is recommended to try it first and then decide.

How to add a new disk to Linux How to add a new disk to Linux Jun 27, 2025 am 12:15 AM

The steps to add a new hard disk to the Linux system are as follows: 1. Confirm that the hard disk is recognized and use lsblk or fdisk-l to check; 2. Use fdisk or parted partitions, such as fdisk/dev/sdb and create and save; 3. Format the partition to a file system, such as mkfs.ext4/dev/sdb1; 4. Use the mount command for temporary mounts, such as mount/dev/sdb1/mnt/data; 5. Modify /etc/fstab to achieve automatic mount on the computer, and test the mount first to ensure correctness. Be sure to confirm data security before operation to avoid hardware connection problems.

Where are system logs located in Linux? Where are system logs located in Linux? Jun 24, 2025 am 12:15 AM

Logs in Linux systems are usually stored in the /var/log directory, which contains a variety of key log files, such as syslog or messages (record system logs), auth.log (record authentication events), kern.log (record kernel messages), dpkg.log or yum.log (record package operations), boot.log (record startup information); log content can be viewed through cat, tail-f or journalctl commands; application logs are often located in subdirectories under /var/log, such as Apache's apache2 or httpd directory, MySQL log files, etc.; at the same time, it is necessary to note that log permissions usually require s

See all articles