1.什麼是sed
sed,流編輯器,即stream editor。它可以将文本檔案的每一行讀取到記憶體,即所謂sed的模式空間,在這個模式空間中可以進行編輯并輸出。
2.sed的使用格式
說明:
a.Adress實際上是用來确定編輯檔案的範圍,可以是精确的某一行,也可以是從某一行到某一行,也 可以用正則進行過濾比對。
b.Command表示将符合Address的行進行XXX操作。注意預設情況下,sed隻對模式空間的内容進行編輯并不會直接處理文本檔案。
c.常用options有,-n (表示靜默模式), -i(将對文本檔案中的内容進行直接修改) 等。
3.sed的使用心得
心得一:如果我們不給address,那麼表示全部,p指令表示print pattern space,是以會列印兩份。p指令常與-n選項進行配合。
心得二:Address常用的表示法有
startLine,endLine
startLine,+length
/regexp1/,/regexp2/
lineNumber
/regexp/
另外$表示最後一行
心得三:預設基本正則,支援擴充正則-r。分組及引用的概念也都适用。
心得四:常用COMMAND總結
d:delete current pattern space
p:print pattern space
a \string:append string to current pattern space
i \string:insert string to current pattern space
r file:read file content and append to current pattern space
w file:write current pattern space to file
s /regexp/string/:find lines that match the regexp and replace with the string in first
s /regexp/string/g:global
s /regexp/string/i:ingore case
s #regexp#string#:this # equals /
本文出自 “” 部落格,請務必保留此出處