sed 修改檔案

馬昌偉 發表於 2021-12-07

 

 

總結

正確的修改進檔案命令(替換檔案內容):
sed -i "s#machangwei#mcw#g" mcw.txt

正確的修改追加進檔案命令(追加檔案內容):
sed -i "\$a 8: wo bu hao\!" mcw.txt
sed -i '$a 7: wo hen hao!' mcw.txt

最後一行$a追加內容,加上-i修改到檔案;單引號無需轉義,雙引號需要注意特殊字元轉義
只有-i和替換,沒有-n和列印,才是我們經常需要的替換。將檔案內容替換並寫入到檔案了
第幾行上面插入字串,但是並不修改原檔案,加上-i也不行,使用單雙引號也不行
匹配指定行上面插入或追加字串,會報錯,加上-i不行,不加還是不行,

替換檔案存在的坑

sed -i "s#machangwei#mcw#g" mcw.txt
只有-i和替換,沒有-n和列印,才是我們經常需要的替換。將檔案內容替換並寫入到檔案了。不能新增-n和列印做sed替換

 

替換實操

首先建立一個測試檔案
[[email protected] ~/mcw]$ mkdir mcw03
[[email protected] ~/mcw]$ cd mcw03/
[[email protected] ~/mcw/mcw03]$ vim mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi machangwei
4: 22bc
5: fff abc
6: abc
[[email protected] ~/mcw/mcw03]$ cp mcw.txt mcw.txt.bak
[[email protected] ~/mcw/mcw03]$ ls
mcw.txt mcw.txt.bak

-n卻沒有列印,不顯示替換結果也不改變檔案
[[email protected]01 ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#g" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi machangwei
4: 22bc
5: fff abc
6: abc

-n沒有列印,就是空內容,加上-i就把空內容加到檔案裡了。檔案就空了,內容被刪除。sed操作前最好先備份
[[email protected] ~/mcw/mcw03]$ sed -ni "s#machangwei#mcw#g" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
[[email protected] ~/mcw/mcw03]$
[[email protected] ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt
[[email protected] ~/mcw/mcw03]$ sed -ni "s#machangwei#mcw#g" mcw.txt
[[email protected] ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt

-n且列印,就顯示替換後的那一行,
[[email protected] ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
3: wo shi mcw

沒有-n,替換然後列印。替換的行是重複列印
[[email protected] ~/mcw/mcw03]$ sed "s#machangwei#mcw#gp" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
3: wo shi mcw
4: 22bc
5: fff abc
6: abc

-n加列印p,只顯示替換的行,並且顯示是替換後的結果
[[email protected] ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
3: wo shi mcw

-n加列印p,本來就顯示替換後的行,加個-i修改檔案,那麼把顯示的替換都的行寫入檔案,相當於重寫檔案或者說把顯示結果覆蓋到原檔案
[[email protected] ~/mcw/mcw03]$ sed -ni "s#machangwei#mcw#gp" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
3: wo shi mcw
[[email protected] ~/mcw/mcw03]$ cp mcw.txt mcw.txt.bak
[[email protected] ~/mcw/mcw03]$ sed -n "s#machangwei#mgp" mcw.txt
sed:-e 表示式 #1,字元 16:未終止的“s”命令
[[email protected] ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
3: wo shi mcw
[[email protected] ~/mcw/mcw03]$ cat mcw.txt.bak
3: wo shi mcw


-i插入,後面還有個p列印,會將替換掉的行重複寫入到檔案
[[email protected] ~/mcw/mcw03]$ vim mcw.txt
[[email protected] ~/mcw/mcw03]$ vim mcw.txt.bak
[[email protected] ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
3: wo shi mcw
[[email protected] ~/mcw/mcw03]$ sed -i "s#machangwei#mcw#gp" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
3: wo shi mcw
4: 22bc
5: fff abc
6: abc

 

[[email protected] ~/mcw/mcw03]$ cat mcw.txt.bak
1: ni hao ya
2:
3: wo shi machangwei
4: 22bc
5: fff abc
6: abc
[[email protected] ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt
[[email protected] ~/mcw/mcw03]$ sed -i "s#machangwei#mcw#gp" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
3: wo shi mcw
4: 22bc
5: fff abc
6: abc

只有-i和替換,沒有-n和列印,才是我們經常需要的替換。將檔案內容替換並寫入到檔案 了
[[email protected] ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt
[[email protected] ~/mcw/mcw03]$ sed -i "s#machangwei#mcw#g" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
[[email protected] ~/mcw/mcw03]$
[[email protected] ~/mcw/mcw03]$ #上面是替換中存在的坑


追加到檔案最後一行實操

準備檔案
[[email protected] ~/mcw/mcw03]$ #下面是插入和追加到檔案
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc

最後一行追加內容,不加-i會顯示追加後的內容,但是不修改檔案。這裡是單引號
[[email protected] ~/mcw/mcw03]$ sed '$a 7: wo hen hao!' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc

最後一行追加內容,加上-i修改到檔案。這裡單引號
[[email protected] ~/mcw/mcw03]$ sed -i '$a 7: wo hen hao!' mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!


[[email protected] ~/mcw/mcw03]$ sed "\$a 8: wo bu hao!" mcw.txt
-bash: !": event not found
[[email protected] ~/mcw/mcw03]$ sed -i '$a 7: wo hen hao\!' mcw.txt
[[email protected] ~/mcw/mcw03]$ vim mcw.txt
[[email protected] ~/mcw/mcw03]$


雙引號最後一行追加,需要將裡面的特殊字串轉義 。不加-i不修改檔案
[[email protected] ~/mcw/mcw03]$ sed "\$a 8: wo bu hao\!" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ #雙引號裡特殊字元會被解析,所以要轉義
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!

雙引號最後一行追加,需要將裡面的特殊字串轉義 。加-i修改了原檔案
[[email protected] ~/mcw/mcw03]$ sed -i "\$a 8: wo bu hao\!" mcw.txt
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ sed "$d" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!


第幾行插入字串,但是並不修改原檔案
[[email protected] ~/mcw/mcw03]$ sed '7i 小馬過河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!

第幾行插入字串,但是並不修改原檔案,加上-i也不行
[[email protected] ~/mcw/mcw03]$ sed -i '7i 小馬過河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i 小馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ sed '7i 小馬過河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i i 過河
7: wo hen hao!
8: wo bu hao!

第幾行上面插入字串,但是並不修改原檔案,加上-i也不行使用雙引號也不行
[[email protected] ~/mcw/mcw03]$ sed "7i 小馬過河" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ sed -i "7i 小馬過河" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i 小馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!

匹配指定行上面插入字串,但是並不修改原檔案,加上-i也不行,使用雙引號也不行,
[[email protected] ~/mcw/mcw03]$ sed "/wo he/i 小馬過河" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i/wo he/i馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03sed '/wo he/i 小馬過河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$
[[email protected] ~/mcw/mcw03]$ sed '/wo he/i 小馬過河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!


匹配指定行上面插入或追加字串,會報錯,加上-i不行,不加還是不行,
[[email protected] ~/mcw/mcw03]$ sed -i '/wo he/i 小馬過河' mcw.txt
> ^C
[[email protected] ~/mcw/mcw03]$ sed '/abc/a 小馬過河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
小馬過河
6: abc
小馬過河
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ sed -i '/abc/a 小馬過河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[[email protected] ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!

 

相關文章