Linux command – sed

Loading

透過 sed (Stream EDitor) 與 awk 都可以把文件做過濾和轉換成新的輸出內容.不過 sed 適合用於一整行的資料處理,而 awkhttps://benjr.tw/97139 則比較適合將一整行做多個 欄位 的資料處理.先來看看 sed 的幾個範例.

# sed [OPTION]... {script-only-if-no-other-script} [input-file]...

先產生一個文字檔當做 sed 過濾和轉換成範例.

root@ubuntu:~# ifconfig ens33 > ens33.txt
root@ubuntu:~# nl ens33.txt 
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:3c:1c:8a  
     2	          inet addr:172.16.15.129  Bcast:172.16.15.255  Mask:255.255.255.0
     3	          inet6 addr: fe80::9500:4488:f396:fea3/64 Scope:Link
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     5	          RX packets:5125 errors:0 dropped:0 overruns:0 frame:0
     6	          TX packets:3127 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	          RX bytes:3386373 (3.3 MB)  TX bytes:358734 (358.7 KB)

sed 常用參數

d

Delete pattern space
範例:刪除第2行

root@ubuntu:~# nl ens33.txt |sed '2d'
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:3c:1c:8a  
     3	          inet6 addr: fe80::9500:4488:f396:fea3/64 Scope:Link
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     5	          RX packets:5125 errors:0 dropped:0 overruns:0 frame:0
     6	          TX packets:3127 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	          RX bytes:3386373 (3.3 MB)  TX bytes:358734 (358.7 KB)

範例:刪除第2~5列

root@ubuntu:~# nl ens33.txt | sed -e '2,5d' 
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:6d:a9:f0  
     6	          TX packets:6827 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	          RX bytes:5276948 (5.2 MB)  TX bytes:914447 (914.4 KB)

a text

Append text which has each embedded newline preceded by a back‐slash \
範例:在第二行插入新字串

root@ubuntu:~# nl ens33.txt | sed '2a testing \
> testing'
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:3c:1c:8a  
     2	          inet addr:172.16.15.129  Bcast:172.16.15.255  Mask:255.255.255.0
testing 
testing
     3	          inet6 addr: fe80::9500:4488:f396:fea3/64 Scope:Link
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     5	          RX packets:5125 errors:0 dropped:0 overruns:0 frame:0
     6	          TX packets:3127 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	          RX bytes:3386373 (3.3 MB)  TX bytes:358734 (358.7 KB)

c text

Replace the selected lines with text (which has each embedded newline preceded by a backslash.
範例:使用新字串取代第二行到第五行

root@ubuntu:~# nl ens33.txt | sed '2,5c testing \
> testing'
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:3c:1c:8a  
testing 
testing
     6	          TX packets:3127 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	          RX bytes:3386373 (3.3 MB)  TX bytes:358734 (358.7 KB)

p

Print the current pattern space.
範例:顯示 2至5行,但其他的行還是會重覆顯示,需要加入參數 -n

root@ubuntu:~# nl ens33.txt | sed '2,5p'
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:3c:1c:8a  
     2	          inet addr:172.16.15.129  Bcast:172.16.15.255  Mask:255.255.255.0
     2	          inet addr:172.16.15.129  Bcast:172.16.15.255  Mask:255.255.255.0
     3	          inet6 addr: fe80::9500:4488:f396:fea3/64 Scope:Link
     3	          inet6 addr: fe80::9500:4488:f396:fea3/64 Scope:Link
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     5	          RX packets:5125 errors:0 dropped:0 overruns:0 frame:0
     5	          RX packets:5125 errors:0 dropped:0 overruns:0 frame:0
     6	          TX packets:3127 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	          RX bytes:3386373 (3.3 MB)  TX bytes:358734 (358.7 KB)

用了參數 -n, –quiet, –silent , suppress automatic printing of pattern space

root@ubuntu:~# nl ens33.txt | sed -n '2,5p'
     2	          inet addr:172.16.15.129  Bcast:172.16.15.255  Mask:255.255.255.0
     3	          inet6 addr: fe80::9500:4488:f396:fea3/64 Scope:Link
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     5	          RX packets:5125 errors:0 dropped:0 overruns:0 frame:0

s/regexp/replacement/

Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement.
範例:使用 Receive 取代 RX 字串,但同一行有多個 RX 只會取代第一個符合的字串.如果要全部取代需要在最後面加入參數 g.

root@ubuntu:~# nl ens33.txt | sed 's/RX/Receive/g' 
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:3c:1c:8a  
     2	          inet addr:172.16.15.129  Bcast:172.16.15.255  Mask:255.255.255.0
     3	          inet6 addr: fe80::9500:4488:f396:fea3/64 Scope:Link
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     5	          Receive packets:5125 errors:0 dropped:0 overruns:0 frame:0
     6	          TX packets:3127 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	          Receive bytes:3386373 (3.3 MB)  TX bytes:358734 (358.7 KB)

如果沒有定義 replacement 就會變成刪除.

root@ubuntu:~# nl ens33.txt | sed 's/RX//g' 
root@ubuntu:~# nl ens33.txt | sed 's/RX//g' 
     1	ens33     Link encap:Ethernet  HWaddr 00:0c:29:6d:a9:f0  
     2	          inet addr:172.16.15.130  Bcast:172.16.15.255  Mask:255.255.255.0
     3	          inet6 addr: fe80::d486:b6ac:858e:5140/64 Scope:Link
     4	          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     5	           packets:11224 errors:0 dropped:0 overruns:0 frame:0
     6	          TX packets:6827 errors:0 dropped:0 overruns:0 carrier:0
     7	          collisions:0 txqueuelen:1000 
     8	           bytes:5276948 (5.2 MB)  TX bytes:914447 (914.4 KB)

-i-i[SUFFIX], –in-place[=SUFFIX]

Edit files in place (makes backup if SUFFIX supplied)
前面的範例都不會對原始檔案做變動,加入參數 -i 可以直接修改原始檔案內容並會保留原始檔案成為新檔案.

root@ubuntu:~# sed 's/RX/Receive/g' ens33.txt 
ens33     Link encap:Ethernet  HWaddr 00:0c:29:34:6a:05  
          inet addr:172.16.15.132  Bcast:172.16.15.255  Mask:255.255.255.0
          inet6 addr: fe80::7d8d:eeb6:e5ba:dae0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Receive packets:3825 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1966 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          Receive bytes:4043870 (4.0 MB)  TX bytes:251228 (251.2 KB)

root@ubuntu:~# cat ens33.txt 
ens33     Link encap:Ethernet  HWaddr 00:0c:29:34:6a:05  
          inet addr:172.16.15.132  Bcast:172.16.15.255  Mask:255.255.255.0
          inet6 addr: fe80::7d8d:eeb6:e5ba:dae0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3825 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1966 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4043870 (4.0 MB)  TX bytes:251228 (251.2 KB)

ens33.txt 原始檔案內容被修改了,並將原本檔案儲存成為 ens33.txt.bak

root@ubuntu:~# sed -i.bak 's/RX/Receive/g' ens33.txt 
root@ubuntu:~# cat ens33.txt 
ens33     Link encap:Ethernet  HWaddr 00:0c:29:34:6a:05  
          inet addr:172.16.15.132  Bcast:172.16.15.255  Mask:255.255.255.0
          inet6 addr: fe80::7d8d:eeb6:e5ba:dae0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Receive packets:3825 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1966 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          Receive bytes:4043870 (4.0 MB)  TX bytes:251228 (251.2 KB)

root@ubuntu:~# ll ens33.txt*
-rw-r--r-- 1 root root 501 Sep 27 21:08 ens33.txt
-rw-r--r-- 1 root root 501 Sep 27 21:08 ens33.txt.bak
沒有解決問題,試試搜尋本站其他內容

One thought on “Linux command – sed

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料