標籤:sed Regex sed編程
sed 是一個非互動式文字編輯器,它可對文字檔和標準輸入進行編輯
sed只是對緩衝區中原始檔案的副本進行編輯,並不編輯原始的檔案
sed命令調用的三種方式
1、在shell命令列輸出命令調用sed
sed [option] ‘sed cmd‘ file
2、將sed命令插入指令碼
sed [option] -f ‘sed.sh‘ file
#vi sed.sh sed要調用的指令碼為sed.sh
#/file:/a\This is a test line!!!
呼叫指令碼
#sed -f sed.sh test
3、將sed命令插入指令碼,直接執行
./sed.sh file
#vi sed.sh sed指令碼
#!/bin/sed -f
#/file:/a\This is a test line!!!
#./sed.sh test 執行指令碼
sed命令選項及意義
-n #不列印所有行到標準輸出
-e #表示將下一個字串解析為sed編輯命令,如果只傳遞一個編輯命令給sed,-e選項可以省略
-f #調用sed指令檔
sed命令定位文本的方法
x #x為指定行號
x,y #指定從x到y的行號範圍
/pattern/ #查詢包含模式的行
/pattern/pattern/ #查詢包含兩個模式的行
/pattern/,x #從與pattern的匹配行到x號行之間的行
x,/pattern/ #從x號行到與pattern的匹配行之間的行
x,y! #查詢不包括x和y行號的行
sed編輯命令
p #列印匹配行
= #列印檔案行號
a\ #在定位行號之後追加文本資訊
i\ #在定位行號之前插入文本資訊
d #刪除定位行
c\ #用新文本替換定位文本
s #使用替換模式替換相應模式
r #從另一個檔案中讀文本
w #將文本寫入到一個檔案
y #變換字元
q #第一個模式比對完成後退出
l #顯示與八進位ASCII碼等價的控制字元
{} #在定位行執行的命令組
n #讀取下一個輸入行,用下一個命令處理新的行
h #將模式緩衝區的文本複製到保持緩衝區
H #將模式緩衝區的文本追加到保持緩衝區
x #互換模式緩衝區和保持緩衝區的內容
g #將保持緩衝區的內容複寫到模式緩衝區
G #將保持緩衝區的內容追加到模式緩衝區
======================================================================================
sed樣本
列印第一行
#sed -n ‘1p‘ test
列印全部內容,並且有兩個第一行
#sed ‘1p‘ test
列印文本中的3-6行
#sed -n ‘3,6p‘ test
模式比對,列印出包含certificate所在的行
#sed -n ‘/certificate/p‘ test
sed [-e]:表示將下一個字串解析為sed編輯命令
(1)、列印certificate匹配模式的內容
(2)、列印certificate匹配模式的行號
(3)、列印certificate匹配模式的內容及行號
#sed -n ‘/certificate/p‘ test
#sed -n ‘/certificate/=‘ test
#sed -n -e ‘/certificate/p‘ -e ‘/certificate/=‘ test
帶多個編輯命令sed的一般格式為
#sed [option] -e cmd1 -e cmd2 -e cmdn inputfile
在文本字串file:後面追加內容為:This is a test file!!!
#sed ‘/file:/a\This is a test file !!!‘ test
將wanglei使用者直接追加到/etc/passwd文本的root下面
#sed -i ‘/root:x:0:0/a\wanglei:x:0:0:wanglei:/wanglei:/bin/bash‘ /etc/passwd
列印出文本test中包含.字元的行
#sed -n ‘/\./p‘ test
列印出文本test中包含$字元的行
#sed -n ‘/\$/p‘ test
列印出文本test中的最後一行
#sed -n ‘$p‘ test
列印出以bus結尾字串的行
#sed -n ‘/.*bus$/p‘ test
列印出不在2-10之間的行
#sed -n ‘2,10!p‘ test
列印出除了字串certificate以外的所有行
#sed -n ‘/certificate/!p‘ test
在文本test中above字串前面的行插入內容"This is a test line!!!",並儲存於檔案之中
#sed -i ‘/above/i\@This is a test line!!!‘ test
列印在文本test中above字串到13行之間的內容
#sed -n ‘/above/,13p‘ test
列印在文本test中3行到字串/home/globus/.的行
#sed -n ‘3,/\/home\/globus\/\./p‘ test
在test文本$88字串前面插入一行"This is a test \i line!!!"
#sed ‘/\$88/i\This is a test \\i line!!!‘ test
把文中匹配的字串的文本行用新文本替代\c
#sed ‘/指定地址/c\test‘ file
將文本中匹配above字串的行替換為新行"This is a new line!!!"
#sed ‘/above/c\This is a new line!!!‘ test
將文本中1,5,13行替換為"This is a 1 line !!!"(1,5,13)
#sed -e ‘1c\This is a 1 line!!!‘ -e ‘5c\This is a 5 line!!!‘ -e ‘13c\This is a 13 line!!!‘ test
刪除test文本中的第3行
#sed ‘3d‘ test
刪除test文本中的1-3行
#sed ‘1,3d‘ test
刪除test文本中字串above到13行的內容
#sed ‘/above/,13d‘ test
刪除最後一行文本
#sed ‘$d‘ test
刪除5行到最後一行
#sed ‘5,$d‘ test
刪除文本中不區分大小寫字串certificate的行
#sed ‘/[Cc][Ee][Rr][Tt][Ii][Ff][Ii][Cc][Aa][Tt][Ee]/d‘ test
替換文本 #替換一個或多個字串
s/被替換的字串/新字串/[替換選項]
g #表示替換文本中所有出現被替換字串之處
p #與-n選項結合,只列印替換行
w 檔案名稱 #表示將輸出定向到一個檔案
#sed -n ‘s/被替換的字串/新字串/p‘ 輸入檔案
將test檔案中的字串Certificate替換成CERTIFICATE(同行中有多個則只替換前面的一個)
#sed -n ‘s/certificate/CERTIFICATE/p‘ test
將test檔案中所有的字串above替換成shabi,並儲存於/root/char檔案中
#sed -n ‘s/above/shabi/pg w /root/char‘ test
替換test文本中的字串,將above替換為char,替換文中每行第二個出現的char
#sed -n ‘s/above/char/2p‘ test
替換test文本中的字串,將above替換為charset,替換文中每行第6個出現的above,並且儲存到/root/save
#sed -n ‘s/above/charset/6p w /root/save‘ xx
替換test文中的字串CERTIFICATE,替換為oye,並儲存為oye
#sed -n ‘s/CERTIFICATE/oye/pg w oye‘ test
& 可以用來儲存被替換的字串以供調用,&符號等價於替換前字串
#sed -n ‘s/seu/(&)/pg‘ test
#sed -n ‘s/seu/(seu)/pg‘ test
列印出test檔案的1-5行,並寫入到output
#sed -n ‘1,5 w output‘ test
將檔案xx的內容讀入檔案test檔案字串above的後面
#sed ‘/above/r xx‘ test
將檔案xx的內容讀入檔案test檔案第5行後面
#sed ‘5r xx‘ test
將檔案xx的內容讀入檔案test檔案第5-7行後面
#sed ‘5,7r xx‘ test
將檔案xx的內容讀入檔案test檔案第5,7,12行後面
#sed -e ‘5r xx‘ -e ‘7r xx‘ -e ‘12r xx‘ test
列印出test檔案中.r.*匹配的檔案
#sed -n ‘/.r.*/p‘ test
列印出test檔案中.r.*匹配的檔案,並且只列印出匹配的第一個行
#sed ‘/.r.*/q‘ test
==========================================================
變換命令 y
表示字元變換,將一系列的字元變換為相應的字元,對字元逐個處理,1處理1 2處理2 …
#sed ‘y/被變換的字元序列/變換的字元序列‘ 輸入檔案
將test檔案中的所有a、b、c、d、e、f、g、h、i分別變換為數字1、2、3、4、5、6、7、8、9
#sed ‘y/abcdefghi/123456789/‘ test
將test檔案中的a、b、o、v、e、字元轉換為A、B、O、V、E
#sed ‘y/above/ABOVE/‘ test
將test檔案中27個英文字母替換為1234567890-=`[email protected]#$%^&*()_+/
#sed ‘y/abcdefghijklmnopqrstuvwxyz/1234567890-=`[email protected]#$%^&*()_+/‘ test
在定位行執行命令組{}
#sed -n ‘/Certificate/{p;=}‘ test
等價於:sed -n -e ‘/Certificate/p‘ -e ‘/Certificate/=‘ test
在test文本中與Certificate關鍵字匹配的行全部的i替換為I,將第一個le替換為99
#sed ‘/Certificate/{s/i/I/g;s/le/99/;}‘ test
n #處理匹配行的下一行
將test文本中字串certificate下面一行的user字元替換為99
#sed ‘/certificate/{n;s/user/99/;}‘ test
將test文本中字串certificate下面一行的\字元替換為gang
#sed ‘/certificate/{n;s/\\/gang/;}‘ test
=================================
sed緩衝區處理
#sed -e ‘/file1/h‘ -e ‘/file2/x‘ -e ‘$G‘ xxx
1、h #將匹配file1的行複製到保持緩衝區
2、x #將匹配file2的行和保持緩衝區的行互換(file1行)
3、G #將保持緩衝區的內容追加到模式緩衝區 $為最後一行
利用分號分隔多個編輯命令 -e {} ;
利用分號列印出test檔案中字串Ceritificate的行和行號
#sed -n ‘/Certificate/p;/Certificate/=‘ test
把xxx檔案中匹配file1的字元替換為wl,匹配file2替換為fx,匹配file3替換為ywc,匹配file4替換為ly
#sed ‘s/file1/wl/; s/file2/fx/; s/file3/ywc/; s/file4/ly/;‘ xxx
以下是前面執行個體有用到的指令碼
======================================================================
1---This is a read file1
2---This is a read file2
3---This is a read file3
4---This is a read file4
5---This is a read file5
6---This is a read file6
7---This is a read file7
======================================================================
1\This is a Certificate Request file:
2\
3\It should be meailed to [email protected]
4\
5\===========================================================
6\Certificate Subject:
7\
8\ /O=Grid/OU=GlobusTest/OU=simpleCA-seugridl.seu.edu.cn/OU=seu.edu.cn/CN=globus
9\
10\the above string is known as your user certificate subject , and it uniquely identifies
11\this user . $88
12\To install this user certificate, please save this e-mail message into the following file.
13\
14\/home/globus/.globus/usercert.pem
15\fawnbusgfqa
16\fwafawfbus
Shell編程之---sed命令詳解