linux sed命令總結
簡介
sed 是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩衝區中,稱爲“模式空間”(pattern space),接着用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往屏幕。接着處理下一行,這樣不斷重複,直到文件末尾。文件內容並沒有 改變,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件;簡化對文件的反覆操作;編寫轉換程序等。
[root@www ~]# sed [-nefr] [動作] 選項與參數: -n :使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN 的數據一般都會被列出到終端上。但如果加上 -n 參數後,則只有經過sed 特殊處理的那一行(或者動作)纔會被列出來。 -e :直接在命令列模式上進行 sed 的動作編輯; -f :直接將 sed 的動作寫在一個文件內, -f filename 則可以運行 filename 內的 sed 動作; -r :sed 的動作支持的是延伸型正規表示法的語法。(默認是基礎正規表示法語法) -i :直接修改讀取的文件內容,而不是輸出到終端。 動作說明: [n1[,n2]]functionn1, n2 :不見得會存在,一般代表『選擇進行動作的行數』,舉例來說,如果我的動作是需要在 10 到 20 行之間進行的,則『 10,20[動作行爲] 』function: a :新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行); c :取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行; d :刪除,因爲是刪除啊,所以 d 後面通常不接任何咚咚; i :插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行); p :列印,亦即將某個選擇的數據印出。通常 p 會與參數 sed -n 一起使用; s :取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法!例如 1,20s/old/new/g;
舉例:(假設我們有一文件名爲test.txt)
刪除某行
[root@localhost ruby] # sed '1d' test.txt #刪除第一行
[root@localhost ruby] # sed '$d' test.txt #刪除最後一行
[root@localhost ruby] # sed '1,2d' test.txt #刪除第一行到第二行
[root@localhost ruby] # sed '2,$d' test.txt #刪除第二行到最後一行
顯示某行
. [root@localhost ruby] # sed -n '1p' test.txt #顯示第一行
[root@localhost ruby] # sed -n '$p' test.txt #顯示最後一行
[root@localhost ruby] # sed -n '1,2p' test.txt #顯示第一行到第二行
[root@localhost ruby] # sed -n '2,$p' test.txt #顯示第二行到最後一行
使用模式進行查詢
[root@localhost ruby] # sed -n '/ruby/p' test.txt #查詢包括關鍵字ruby所在所有行
[root@localhost ruby] # sed -n '/\$/p' test.txt #查詢包括關鍵字$所在所有行,使用反斜線\屏蔽特殊含義
以行爲單位的新增/刪除
[root@www script]# cat -n /etc/passwd | sed '2,4d' #將2-4行內容刪除了。
1 root:x:0:0:root:/root:/bin/bash
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
....省略部分內容......
cat -n /etc/passwd | sed '2i 111111' #在第二行前加上111111字符串
[root@www script]# cat -n /etc/passwd|sed '2,5c 2-5 number' 將2-5行替換爲2-5 number
1 root:x:0:0:root:/root:/bin/bash
2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.....(後面省略).....
數據的搜尋並顯示
搜索 /etc/passwd有root關鍵字的行
[root@www script]# cat -n /etc/passwd|sed '/root/p'
1 root:x:0:0:root:/root:/bin/bash 列出匹配行後還會列出所有的行
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
.....(後面省略).....
加-n選項的時候將只打印包含模板的行。
[root@www script]# cat -n /etc/passwd|sed -n '/root/p'
1 root:x:0:0:root:/root:/bin/bash
11 operator:x:11:0:operator:/root:/sbin/nologin
數據的搜尋並刪除
刪除/etc/passwd所有包含root的行,其他行輸出
nl /etc/passwd | sed '/root/d'
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3 bin:x:2:2:bin:/bin:/bin/sh
.....(後面省略).....
#第一行的匹配root已經刪除了
數據的搜尋並執行命令
搜索/etc/passwd,找到root對應的行,執行後面花括號中的一組命令,每個命令之間用分號分隔,這裏把bash替換爲blueshell,再輸出這行:
[root@www script]# nl /etc/passwd|sed -n '/root/{s/bash/blueshell/;p}'
1 root:x:0:0:root:/root:/bin/blueshell
11 operator:x:11:0:operator:/root:/sbin/nologin
如果只替換/etc/passwd的第一個bash關鍵字爲blueshell,就退出
[root@www script]# nl /etc/passwd|sed -n '/root/{s/bash/blueshell/;p;q}' #最後的q是退出
1 root:x:0:0:root:/root:/bin/blueshell
數據的搜尋和替換
sed 's/要被取代的字串/新的字串/g'
利用ifconfig 查詢 IP信息
[root@www script]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:5D:67:9B
inet addr:192.168.121.128 Bcast:192.168.121.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5d:679b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14775 errors:0 dropped:0 overruns:0 frame:0
TX packets:6470 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1188379 (1.1 MiB) TX bytes:778349 (760.1 KiB)
過濾出IP
[root@www script]# ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g'
192.168.121.128 Bcast:192.168.121.255 Mask:255.255.255.0
將 IP 後面的部分予以刪除
[root@www script]# ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g'|sed 's/Bcast.*$//g'
192.168.121.128
多點編輯
一條sed命令,刪除/etc/passwd第三行到末尾的數據,並把bash替換爲blueshell
[root@www script]# cat -n /etc/passwd|sed -e '4,$d' -e 's/nologin/haha/g'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/haha
3 daemon:x:2:2:daemon:/sbin:/sbin/haha
-e表示多點編輯,第一個編輯命令刪除/etc/passwd第四行到末尾的數據,第二條命令搜索nologin替換爲haha。
[root@www ~]# cat test.txt
asfkjlsfj
asfkjls
asfkjlswer
fkjlswer
fkjlswerhfh
fkjlerhfh
[root@www ~]# sed '1a ljx tea' test.txt #第一行後增加字符串"ljx tea"
asfkjlsfj
ljx tea
asfkjls
asfkjlswer
fkjlswer
fkjlswerhfh
fkjlerhfh
[root@www ~]# sed '1,3a ljx tea' test.txt #第一行到第三行後增加字符串"ljx tea"
asfkjlsfj
ljx tea
asfkjls
ljx tea
asfkjlswer
ljx tea
fkjlswer
fkjlswerhfh
fkjlerhfh
[root@www ~]# cat -n /etc/passwd | sed '2a Drink tea or ......\
> drink beer ?'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ......
drink beer ?
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(後面省略).....
[root@www ~]# sed '1a ljx tea\nor lll' test.txt #第一行後增加多行,使用換行符\n
asfkjlsfj
ljx tea
or lll
asfkjls
asfkjlswer
fkjlswer
fkjlswerhfh
fkjlerhfh
代替一行或多行
[root@www ~]# sed '1c Hi' test.txt #第一行代替爲Hi
Hi
asfkjls
asfkjlswer
fkjlswer
fkjlswerhfh
fkjlerhfh
[root@www ~]# sed '1,3c Hi' test.txt #第一行到第三行代替爲Hi
Hi
fkjlswer
fkjlswerhfh
fkjlerhfh
替換一行中的某部分
格式:sed 's/要替換的字符串/新的字符串/g' (要替換的字符串可以用正則表達式)
[root@www ~]# sed -n '/fk/p' test.txt |sed 's/fk/iii/g' #替換fk爲iii
asiiijlsfj
asiiijls
asiiijlswer
iiijlswer
iiijlswerhfh
iiijlerhfh
[root@www ~]# sed -n '/fk/p' test.txt |sed 's/fk//g' #刪除fk
asjlsfj
asjls
asjlswer
jlswer
jlswerhfh
jlerhfh
插入
[root@localhost ruby] # sed -i '$a bye' test.txt #在文件ab中最後一行直接輸入"bye"
[root@localhost ruby]# cat test.txt
Hello!
ruby is me,welcome to my blog.
end
bye
刪除匹配行
sed -i '/匹配字符串/d' filename (注:若匹配字符串是變量,則需要“”,而不是‘’。記得好像是)
替換匹配行中的某個字符串
sed -i '/匹配字符串/s/替換源字符串/替換目標字符串/g' filename