替換
創(chuàng)建 dog.txt
文件捶牢,輸入如下內(nèi)容
I have a dog, my cat's name is xxx.
I love my dog.
My dog love my too.
My dog was 2 years old.
替換每一行中第一次出現(xiàn)的 dog
為 cat
。(注 在替換的時(shí)候巍耗,不僅僅是 /
可作為分隔符秋麸, 其他字符也可作為分隔符, 只需要前后的分隔符一致即可)
sed 's/dog/cat/' dog.txt
結(jié)果為:
I have a cat, my cat's name is xxx.
I love my cat.
My cat love my too.
My cat was 2 years old.
全部替換
g
表示把左右的 dog
替換為 cat
炬太。
$ sed 's/dog/cat/g' dog.txt
結(jié)果為:
I have a cat, my cat's name is xxx.
I love my cat.
My cat love my too.
My cat was 2 years old.
替換指定內(nèi)容
替換第2行
$ sed "2s/dog/cat/g" dog.txt
I have a dog, my cat's name is xxx.
I love my cat.
My dog love my too.
My dog was 2 years old.
替換3-5行
$ sed '3,5s/dog/cat/g' dog.txt
I have a dog, my cat's name is xxx.
I love my dog.
My cat love my too.
My cat was 2 years old.
替換最后一行
$ sed '$s/dog/cat/g' dog.txt
I have a dog, my cat's name is xxx.
I love my dog.
My dog love my too.
My cat was 2 years old.
替換第2行到最后一行
$ sed '2,$s/dog/cat/g' dog.txt
I have a dog, my cat's name is xxx.
I love my cat.
My cat love my too.
My cat was 2 years old.
只替換每一行第一個(gè)o
$ sed 's/o/O/1' dog.txt
I have a dOg, my cat's name is xxx.
I lOve my dog.
My dOg love my too.
My dOg was 2 years old.
只替換每一行第一個(gè)o
sed 's/o/O/2' dog.txt
I have a dog, my cat's name is xxx.
I love my dOg.
My dog lOve my too.
My dog was 2 years Old.
替換源文件
使用 -i
參數(shù)編輯源文件
sed -i "s/dog/cat/g" dog.txt
在 Mac
上會(huì)出現(xiàn)如下錯(cuò)誤
sed: 1: "dog.txt": extra characters at the end of d command
原因:
sed
編輯源文件的時(shí)候灸蟆,需要指定一個(gè)文件后綴,sed
把源文件添加后綴后進(jìn)行備份亲族,如果后綴名稱長度為 0 炒考,則不會(huì)進(jìn)行備份。
Edit files in-place, saving backups with the specified extension.
If a zero-length extension is given, no backup will be saved. It
is not recommended to give a zero-length extension when in-place
editing files, as you risk corruption or partial content in situ-
ations where disk space is exhausted, etc.
解決方式: 添加長度為 0
的后綴名稱孽水。
sed -i '' "s/dog/cat/g" dog.txt
正則表達(dá)式
-
^
表示一行的開頭票腰。如:/^#/
以#
開頭的匹配。 -
$
表示一行的結(jié)尾女气。如:/}$/
以}
結(jié)尾的匹配。 -
\<
表示詞首测柠。 如:\<abc
表示以abc
為首的詞炼鞠。 -
\>
表示詞尾。 如:abc\>
表示以abc
結(jié)尾的詞轰胁。 -
.
表示任何單個(gè)字符谒主。 -
*
表示某個(gè)字符出現(xiàn)了0次或多次。 -
[ ]
字符集合赃阀。 如:[abc]
表示匹配a或b或c霎肯,還有[a-zA-Z]
表示匹配所有的26個(gè)字符擎颖。如果其中有^表示反,如[^a]
表示非a的字符观游。 -
&
保存搜索字符用來替換其他字符搂捧,如s/love/-&-/
,love替換成-love-
懂缕。
多個(gè)匹配
-e
選項(xiàng)允許在同一行里執(zhí)行多條命令:(第 1 行到第 3 行的 dog
替換成 cat
允跑,第 3 行到最后一行的 My
替換成 Her
)
$ sed -e '1,3s/dog/cat/g' -e '3,$s/My/Her/g' dog.txt
I have a cat, my cat's name is xxx.
I love my cat.
Her cat love my too.
Her dog was 2 years old.
使用 &
來當(dāng)做被匹配的變量, 在該變量左右添加其他字符
$ sed 's/My/**&**/g' dog.txt
I have a dog, my cat's name is xxx.
I love my dog.
**My** dog love my too.
**My** dog was 2 years old.
插入
在第 1 行上插入 I have a cat.
。(1 表示第1行搪柑, i 表示插入的意思聋丝。在 Mac 上, i\
之后需要換行工碾,原因參考這里)
$ sed '1 I\
I have a cat. ' dog.txt
I have a cat. I have a dog, my cat's name is xxx.
I love my dog.
My dog love my too.
My dog was 2 years old.
追加
在最后一行追加 Test append.
弱睦。(在 Mac 上,a\
之后需要換行渊额, 具體原因同上)
sed '$ a\
Test append.' dog.txt
I have a dog, my cat's name is xxx.
I love my dog.
My dog love my too.
My dog was 2 years old.
Test append.
刪除操作
刪除空白行:
sed '/^$/d' file
刪除文件的第2行:
sed '2d' file
刪除文件的第2行到末尾所有行:
sed '2,$d' file
刪除文件最后一行:
sed '$d' file
刪除文件中所有開頭是test的行:
sed '/^test/'d file
Pattern Space
和 Hold Space
理解
sed
會(huì)逐行處理文件况木, 首先 sed
把當(dāng)前正在處理的行保存在一個(gè)臨時(shí)緩存區(qū)中(Pattern Space
),然后處理臨時(shí)緩沖區(qū)中的行端圈,完成后把該行發(fā)送到屏幕上焦读。sed
每處理完一行就將其從臨時(shí)緩沖區(qū)刪除,然后將下一行讀入舱权,進(jìn)行處理和顯示矗晃。處理完輸入文件的最后一行后,sed
便結(jié)束運(yùn)行宴倍。
Hold Space
能夠長期存貯 sed
讀取的數(shù)據(jù)张症,當(dāng) sed 在其他行處理我們可以重用 Hold Space
空間里面的數(shù)據(jù), 但是不能直接訪問 Hold Space
內(nèi)的數(shù)據(jù)鸵贬,而是如果要對(duì)其執(zhí)行某些操作俗他,則需要將其復(fù)制或附加到 Pattern Space
。
g
: 將 hold space
中的內(nèi)容拷貝到 pattern space
中阔逼,原來 pattern space
里的內(nèi)容清除;
G
: 將 hold space
中的內(nèi)容append到 pattern space
后
h
: 將 pattern space
中的內(nèi)容拷貝到 hold space
中兆衅,原來的 hold space
里的內(nèi)容被清除
H
: 將 pattern space
中的內(nèi)容append到 hold space
后
x
: 交換 pattern space
和 hold space
的內(nèi)容
使用如下示例文件
$ cat t.txt
one
two
three
反序輸出文件每一行的內(nèi)容:(參考sed 簡明教程)
$ sed '1!G;h;$!d' t.txt
three
two
one
其中的 ‘1!G;h;$!d’ 可拆解為三個(gè)命令
- 1!G —— 只有第一行不執(zhí)行G命令,將hold space中的內(nèi)容append回到pattern space
- h —— 第一行都執(zhí)行h命令嗜浮,將pattern space中的內(nèi)容拷貝到hold space中
- $!d —— 除了最后一行不執(zhí)行d命令羡亩,其它行都執(zhí)行d命令,刪除當(dāng)前行
參考資料:
sed use: expected context address
The concept of "Hold space" and "Pattern space" in sed