sed 是什么?
sed是一種流編輯器,它是文本處理中非常中的工具,在linux中被稱為linux文本處理三劍客之一,能夠完美的配合正則表達式使用,功能不同凡響。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處
理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容并沒有改變,除非你使用重定向存儲輸出。
sed工作模型
sed的基本語法
sed [OPTION]... {script-only-if-no-other-script} [input-file]..
opotion 的選項:
-n 靜默模式,不輸出模式空間的內容
[root@localhost ~]#sed '1p' /etc/issue \S \S Kernel \r on an \m [root@localhost ~]#sed -n '1p' /etc/issue \S
-e script -e script 同時執行多個腳本
[root@localhost ~]#sed -e '1s/bash$/zsh/' -e 's/^root/toor/' /etc/passwd toor:x:0:0:root:/root:/bin/zsh
-f /path/to/script 讀取文件中的sed命令
[root@localhost ~]#cat p.sed 1p 2p $p [root@localhost ~]#sed -n -f p.sed /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin apache:x:1008:1008::/home/apache:/sbin/nologin
-i 直接修改原文件,慎用
[root@localhost ~]#sed -n '/^zgx/p' /etc/passwd zgx:x:1007:1007::/home/zgx:/bin/bash [root@localhost ~]#sed -i '/^zgx/d' /etc/passwd [root@localhost ~]#sed -n '/^zgx/p' /etc/passwd
-r 使用擴展正則表達式
[root@localhost ~]#sed -n '/^ro\\+t/p' /etc/passwd root:x:0:0:root:/root:/bin/bash [root@localhost ~]#sed -n '/^ro+t/p' /etc/passwd [root@localhost ~]#sed -n -r '/^ro+t/p' /etc/passwd root:x:0:0:root:/root:/bin/bash
script的表示方法
address+command
address:
1、startline,endline 指定范圍
[root@localhost ~]#sed -n '1,2p' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
2、/regexp/ 正則匹配
[root@localhost ~]#sed -n '/^root/p' /etc/passwd root:x:0:0:root:/root:/bin/bash
3、/pattern1/,/pattern2/ 正則匹配范圍
[root@localhost ~]#sed -n '/^root/,/^lp/p' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
4、linenumber 指定行
[root@localhost ~]#sed -n '10p' /etc/passwd operator:x:11:0:operator:/root:/sbin/nologin
5、startline,+n 指定起始及向后行數
[root@localhost ~]#sed -n '10,+2p' /etc/passwd operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@localhost ~]#sed -n '/^root/,+2p' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
6、步進行
[root@localhost ~]#sed -n '1~2p' /etc/passwd #輸出奇數行 root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown mail:x:8:12:mail:/var/spool/mail:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin
command:
d 刪除符合條件的行
[root@localhost ~]#sed '1d' /etc/passwd #刪除了第一行的root用戶 bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
p 顯示符合條件的行
[root@localhost ~]#sed '1p' /etc/issue #模式空間與匹配到的行都輸出 \S \S Kernel \r on an \m
a \string 在指定的行后面追加新行
[root@localhost ~]#sed '1a\newline' /etc/issue \S newline Kernel \r on an \m
i \string 在指定的行前面插入新行
[root@localhost ~]#sed '1i\newline' /etc/issue newline \S Kernel \r on an \m
c \string 將匹配到的行替換為此處指定的文本text
[root@localhost ~]#sed '1c\newline' /etc/issue newline Kernel \r on an \m
r file 讀取文件,合并文本
[root@localhost ~]#sed '1r /etc/issue' /etc/issue \S \S Kernel \r on an \m Kernel \r on an \m
w file 將地址指定的訪問的行另存為指定文件中
[root@localhost ~]#sed '1,2 w /tmp/passwd' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@localhost ~]#cat /tmp/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
= 為模式匹配到的行打印出行數
[root@localhost ~]#sed '1,2=' /etc/passwd 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin
! 取反
[root@localhost ~]#sed -n '1!p' /etc/issue Kernel \r on an \m
s/pattern/string/修飾符:查找并替換,默認只替換每一行被匹配到的字符串
修飾符:
g:全局替換
[root@localhost ~]#cat root.txt root root txt txt test test ROOT [root@localhost ~]#sed 's/^root/toor/g' root.txt toor toor txt txt test test ROOT
i 忽略帶下寫
[root@localhost ~]#sed 's/^root/toor/i' root.txt toor toor txt txt test test toor
w /path/to/somefile 將替換成功的結果保存至指定文件中
[root@localhost ~]#sed 's/^root/toor/w /tmp/root.txt' root.txt toor toor txt txt test test ROOT [root@localhost ~]#cat /tmp/root.txt toor toor
p 顯示替換成功的行
[root@localhost ~]#sed -n 's/^root/toor/p' root.txt toor toor
高級編輯命令(不常用)
-
h:表示把模式空間的內容保存至保持空間
-
H:把模式空間中的內容追加至保持空間
-
g:把保持空間的內容覆蓋至模式空間
-
G:把保持空間的內容追加至模式空間
-
x:把模式空間的內容與保持空間中內容互換
-
n:讀取匹配到行的下一行至模式空間
-
N: 追加讀取匹配到的行的下一行至模式空間中
-
D:刪除多行模式空間中所有行
實例:
sed -n 'n;p' file 顯示偶數行
sed '1!G;h;$!d' file 逆序顯示文件
sed '$!d' file 取出最后一行
sed '$!N;$!D' FILE 取出最后兩行
sed '/^$/d;G' file 刪除所以空白行,為每行加空白行
sed 'n;d' file:顯示奇數行
原創文章,作者:N25_隨心,如若轉載,請注明出處:http://www.www58058.com/67014
總結的很好,圖文并茂,加油!?。?/p>