技能篇:Sed教程-Linux命令
本文轉(zhuǎn)載自微信公眾號(hào)「潛行前行」,作者cscw。轉(zhuǎn)載本文請(qǐng)聯(lián)系潛行前行公眾號(hào)。
前言
sed 全名為 stream editor,是用于文本處理的流編輯器,支持正則表達(dá)式。sed處理文本時(shí)是一次處理一行內(nèi)容
sed語(yǔ)法
- sed命令處理的內(nèi)容是模式空間中的內(nèi)容,而非直接處理文件內(nèi)容。如果加上參數(shù) i 則可直接修改文件內(nèi)容
- 示例:sed -i 's/原字符串/新字符串/' /home/test.txt
sed [-nefr參數(shù)] [function] [filePath]
選項(xiàng)與參數(shù) | 描述 |
---|---|
-n | 使用 silent 模式。在一般 sed 的用法中,輸入的數(shù)據(jù)都會(huì)被輸出到屏幕上。但如果加上 -n 參數(shù)后,則不會(huì)顯示,如果有跟著 p 標(biāo)志,被 sed 特殊處理的那一行會(huì)被列出來 |
-e | 直接在命令行界面上進(jìn)行 sed 的動(dòng)作編輯,執(zhí)行多條子命令 |
-f | 將 sed 的動(dòng)作寫在一個(gè)文件內(nèi), -f filename 執(zhí)行腳本文件的 sed 動(dòng)作 |
-r | sed 的動(dòng)作支持的是延伸型正則表達(dá)式的語(yǔ)法 |
-i | 直接修改讀取的文件內(nèi)容 |
選項(xiàng)-n,加上-n選項(xiàng)后被設(shè)定為安靜模式,也就是不會(huì)輸出默認(rèn)打印信息,除非子命令中特別指定打印 p 選項(xiàng),則只會(huì)把匹配修改的行進(jìn)行打印
- ---- 兩行都打印出來 ----
- server11:~/test # echo -e 'hello \n world' | sed 's/hello/csc/'
- csc
- world
- ---- 一行也沒打印 -----
- server11:~/test # echo -e 'hello \n world' | sed -n 's/hello/csc/'
- ---- 打印了匹配行 -----
- server11:~/test # echo -e 'hello \n world' | sed -n 's/hello/csc/p'
- csc
選項(xiàng)-e,多條子命令連續(xù)進(jìn)行操作
- echo -e 'hello world' | sed -e 's/hello/csc/' -e 's/world/lwl/'
- 結(jié)果:csc lwl
選項(xiàng)-i,直接修改讀取的文件內(nèi)容
- server11:~/test # cat file.txt
- hello world
- server11:~/test # sed 's/hello/lwl/' file.txt
- lwl world
- server11:~/test # cat file.txt
- hello world
- ---- 加上參數(shù) i 可以直接修改文件內(nèi)容----
- server11:~/test # sed -i 's/hello/lwl/' file.txt
- lwl world
- server11:~/test # cat file.txt
- lwl world
選項(xiàng)-f,執(zhí)行文件腳本
- sed.script腳本內(nèi)容:
- s/hello/csc/
- s/world/lwl/
- ------
- echo "hello world" | sed -f sed.script
- 結(jié)果:csc lwl
選項(xiàng)-r,默認(rèn)只能支持基本正則表達(dá)式,如果需要支持?jǐn)U展正則表達(dá)式,需要加上 -r
- echo "hello world" | sed -r 's/(hello)|(world)/csc/g'
- csc csc
function表達(dá)式:[n1[,n2]] function or /{pattern}/function
- n1, n2 :可選項(xiàng),一般代表“選擇進(jìn)行function處理的行數(shù)”,舉例來說,如果「function」是需要在 10 到 20 行之間進(jìn)行的,則表示為 10,20 [function]
- 如果需用正則表達(dá)式匹配字符串,則可用 /{pattern}/ 匹配
- test.txt 內(nèi)容
- 111
- 222
- 333
- 444
- ----- 刪除非第2第3行之間的所有行 ----------
- server11:~ # sed -i '2,3!d' test.txt
- server11:~ # cat test.txt
- 222
- 333
- ------ 正則表達(dá)式匹配 ------------
- server11:~ # echo 'clswcl.txt' | sed -nr '/.*/p'
- clswcl.txt // /{pattern}/ = /.*/
function 有以下這些選項(xiàng)
function | 描述 |
---|---|
a | 新增:a 的后面可以接字串,而這些字串會(huì)在新的一行出現(xiàn)(目前的下一行) |
i | 插入:i 的后面可以接字串,而這些字串會(huì)在新的一行出現(xiàn)(目前的上一行) |
c | 取代:c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行 |
d | 刪除:因?yàn)槭莿h除啊,所以 d 后面通常不接任何東西 |
p | 打?。阂嗉磳⒛硞€(gè)選擇的數(shù)據(jù)印出。通常 p 會(huì)與參數(shù) sed -n 一起運(yùn)行 |
s | 取代:可以直接進(jìn)行取代的工作哩!通常這個(gè) s 的動(dòng)作可以搭配正則表達(dá)式!例如:1,20 s/old/new/g |
function:-a,行后插入新行
- sed -i '/特定字符串/a 新行字符串' fileName
function:-i,行前插入新行
- sed -i '/特定字符串/i 新行字符串' fileName
function:-c,修改指定內(nèi)容行
- sed -i '/特定字符串/c csc lwl' fileName
function:-d,刪除特定字符串
- sed -i '/特定字符串/d' fileName
sed s子命令: s/{pattern}/{replacement}/{flags}
- {pattern}是正則表達(dá)式
- 如果{pattern}存在分組,{replacement}中的"\n"代表第n個(gè)分組,"&"代表整個(gè)匹配的字符串。詳情看示例
- flags的參數(shù)如下
flags | 描述 |
---|---|
n | 可以是1-512,表示第n次出現(xiàn)的情況進(jìn)行替換 |
g | 全局更改 |
p | 打印模式空間的內(nèi)容 |
w file | 寫入到一個(gè)文件file中 |
示例
- server11:~ # echo -e 'hello 1112 world' | sed -r 's/([a-z]+)( [0-9]+ )([a-z]+)/&/'
- hello 1112 world
- server11:~ # echo -e 'hello 1112 world' | sed -r 's/([a-z]+)( [0-9]+ )([a-z]+)/\3\2\1/'
- world 1112 hello
參考文章
- sed -i命令詳解及入門攻略[1]
Reference
[1]sed -i命令詳解及入門攻略:
https://blog.csdn.net/qq_33468857/article/details/84324609