功能簡介
sed是一種流編輯器,也是文本處理中非常好的工具,配合正則使用更強大處理時,把當前處理的行存儲在臨時緩衝區中,稱爲“模式空間”,接着用sed命令處理緩衝區的內容,完成後輸出到屏幕,接着處理下一行。文件內容並沒有改變,除非使用-i選項。sed主要用來編輯一個或多個文件,簡化對文件的反覆操作或者用來編寫轉換程序等。
sed功能同awk類似,差別在於,sed簡單,對列處理的功能要差一些,awk功能複雜,對列處理的功能比較強大。
命令格式
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
常用參數
-e #以指定的指令來處理輸入的文本文件
-n #取消默認輸出(如果和p命令同時使用只會打印發生改變的行)
-h #幫助
-V #顯示版本信息
常用動作
a #在當前行下面插入文本
i #在當前行上面插入文本
c #把選定的行改爲新的文本
d #刪除,刪除選擇的行
D #刪除模板塊的第一行
s #替換指定字符
h #拷貝模板塊的內容到內存中的緩衝區
H #追加模板塊的內容到內存中的緩衝區
g #獲得內存緩衝區的內容,並替代當前模板塊中的文本
G #獲得內存緩衝區的內容,並追加到當前模板塊文本的後面
l #列表不能打印字符的清單
n #讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令
N #追加下一個輸入行到模板塊後面並在二者間嵌入一個新行,改變當前行號碼
p #打印匹配的行
P #(大寫)打印模板的第一行
q #退出Sed
b #lable 分支到腳本中帶有標記的地方,如果分支不存在則分支到腳本的末尾
r #file 從file中讀行
t #label if分支,從最後一行開始,條件一旦滿足或者T,t命令,將導致分支到帶有標號的命令處,或者到腳本的末尾
T #label 錯誤分支,從最後一行開始,一旦發生錯誤或者T,t命令,將導致分支到帶有標號的命令處,或者到腳本的末尾
w #file 寫並追加模板塊到file末尾**
W #file 寫並追加模板塊的第一行到file末尾**
! #表示後面的命令對所有沒有被選定的行發生作用**
= #打印當前行號碼**
# #把註釋擴展到下一個換行符以前**
Sed替換命令
g #表示行內全面替換(全局替換配合s命令使用)
p #表示打印行
w #表示把行寫入一個文件
x #表示互換模板塊中的文本和緩衝區中的文本
y #表示把一個字符翻譯爲另外的字符(但是不用於正則表達式)
1 #子串匹配標記
& #已匹配字符串標記
Sed正則
^ #匹配行開始
$ #匹配行結束
. #匹配一個非換行符的任意字符
* #匹配0個或多個字符
[] #匹配一個指定範圍內的字符
[^] #匹配一個不在指定範圍內的字符
(..) #匹配子串
& #保存搜索字符用來替換其他字符
< #匹配單詞的開始
> #匹配單詞的結束
x{m} #重複字符x,m次
x{m,} #重複字符x,至少m次
x{m,n} #重複字符x,至少m次,不多於n次
Sed常用實例
1、替換操作
echo "hello world" |sed 's/ /-/1g'
hello-world
#從第一個空格開始全局替換成-,只不過文本中只有一個空格
2、刪除操作
sed '/^$/d' filename #刪除空白行
sed '2d' filename #刪除第二行
sed '2,$d' filename #刪除第二直到未尾所有行
sed '$d' filename #刪除最後一行
sed '/^test/'d filename #刪除以test開頭行
3、匹配替換
echo "hello world" |sed 's/w+/[&]/g'
[hello] [world]
echo "hello world" |sed 's/w+/"&"/g'
"hello" "world"
#w+匹配每一個單詞,&表示匹配到的字符串
echo AAA bbb |sed 's/([A-Z]+) ([a-z]+)/[2] [1]/'
[bbb] [AAA]
#子串匹配替換
4、選定範圍
sed -n '/= 0/,/max/p' svnserve.conf
#min-encryption = 0
#max-encryption = 256
#所有在=0到max範圍內的行都會被打印出來
5、sed多點編輯功能(-e)
[root@centos001 ~]#cat -n test
1 this is a test file
2 welcome
3 to
4 here
5 hello WORLD
6
7 linux centos6.8
8 redhat
sed -e '2,6d' -e 's/linux centos6.8/Linux Centos6.8/' test
this is a test file
Linux Centos6.8
redhat
#如果兩條命令功能一樣,那麼就需要用到下面的參數
sed --expression='s/linux centos6.8/Linux Centos6.8/' --expression='s/to/TO/' test**
this is a test file
welcome
TO
here
hello WORLD
Linux CenTOs6.8
redhat
6、讀入與寫入
[root@centos001 ~]#cat test1
welcom
to
here
[root@centos001 ~]#sed '/here/r test1' test
this is a test file
welcome
to
here
#welcom
to
here#
hello WORLD
linux centos6.8
redhat
#將test1的文件內容讀取顯示所有匹配here行的後面
sed -n '/centos6.8/w test2' test
[root@centos001 ~]#cat test2
linux centos6.8
#將test文件匹配到centos6.8的所有行都寫入到test2文件中,文件可以不存在.
#如果文件存在,就會被重定向不是追加
7、追加與插入
[root@centos001 ~]#sed '/^l/a2017-08-08' test2
linux centos6.8
2017-08-08
#在匹配以l開頭的行的後面追加2017-08-08
[root@centos001 ~]#sed '1a2017-08-08' test2
linux centos6.8
2017-08-08
#在第一行的後面追加2017-08-08
[root@centos001 ~]#sed '/^l/i2017-08-08' test2
2017-08-08
linux centos6.8
#在匹配以l開頭的行的前面插入2017-08-08
#######以上操作是不會改變文件內容################
[root@centos001 ~]#sed -i '/^l/i2017-08-08' test2
[root@centos001 ~]#cat test2
2017-08-08
linux centos6.8
8、其它命令實例
[root@centos001 ~]#cat -n test2
1 2017-08-08
2 linux centos6.8
3 08
4
5 test
[root@centos001 ~]#**sed '/08/{ n; s/l/L/; }' test2
2017-08-08
Linux centos6.8
08
test
#如果08匹配到就跳到下一行,將小寫l替換成大寫,注意到第三行也是被匹配到
#但是後面的條件不滿足,所有沒有被替換
[root@centos001 ~]#sed '1,4y/8/9/' test2
2017-09-09
linux centos6.9
09
test
#將1至4行所有的數字8替換成9
[root@centos001 ~]#**sed '1q' test2**
2017-08-08
#打印第一行內容後退出
9、打印奇數或公偶數行
[root@centos001 ~]#sed -n 'p;n' test2
20170808
08
[root@centos001 ~]#sed -n 'n;p' test2
linux centos6.8
test
[root@centos001 ~]#sed -n '1~2p' test2
20170808
08
[root@centos001 ~]#sed -n '2~2p' test2
linux centos6.8
test
10、打印匹配字符串行的下一行
[root@centos001 ~]#sed -n '/linux/{n;p}' test2
08
[root@centos001 ~]#awk '/linux/{getline; print}' test2
08
每天一個Linux命令(1):xargs