sed介紹:sed工具主要是替換的文本輸出到屏幕上,並且還有其餘更豐富的功能。
sed命令格式:sed -n 'n' p filename,單引號內的n是一個數字,表示幾行。
-n選項的做用就是隻顯示咱們要打印的行,可有可無的內容不顯示。bash
[root@localhost ~]# mkdir sed #建立一個目錄 [root@localhost ~]# cd sed/ #進入到目錄sed [root@localhost sed]# cp /etc/passwd test.txt #拷貝passwd文件到本目錄下,而且修更名字 [root@localhost sed]# ls #查看當前目錄下都有哪些文件 test.txt [root@localhost sed]# pwd #當前所在的位置 /root/sed
[root@localhost sed]# sed -n '/root/'p test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin #-n選項是指顯示咱們要打印的行。
在grep中使用的特殊字符如(^ $ *等) 均可以在sed中使用。工具
[root@localhost sed]# sed -n '2,3'p test.txt bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost sed]# sed -e '1'p -e '/bus/'p -n test.txt root:x:0:0:root:/root:/bin/bash dbus:x:81:81:System message bus:/:/sbin/nologin #sed -e 實現多個行爲,如我查找第一行,而且 把文檔中含有bus的顯示出來
[root@localhost sed]# sed -n '/bus/'Ip test.txt #這裏必定要加一個大I字母。 dbus:x:81:81:System message bus:/:/sbin/nologin BUS
[root@localhost sed]# sed '1,18'd test.txt chrony:x:998:996::/var/lib/chrony:/sbin/nologin BUS #這裏後面跟選項d,會把1到18行的內容刪除,把後面的內容顯示出來。
這裏參數d表示刪除的東西,它不只能夠刪除指定的單行以及多行,並且能夠刪除匹配某個字符的行,還能夠刪除從某一行開始到文檔最後一行的全部行,這個命令僅僅是在屏幕上並不顯示這些行。學習
首先先拷貝一份文件過來: [root@localhost sed]# cp test.txt test.txt.bak #拷貝過來個備份 [root@localhost sed]# ls #查看當前目錄下的文件 test.txt test.txt.bak [root@localhost sed]# wc -l test.txt #wc查看一個文件有多少行 20 test.txt [root@localhost sed]# sed -i '1,18'd test.txt #-i刪除1到18行。 [root@localhost sed]# wc -l test.txt #再來看這個文件有多少行。 2 test.txt
[root@localhost sed]# sed '/root/'d test.txt chrony:x:998:996::/var/lib/chrony:/sbin/nologin BUS
[root@localhost sed]# sed '1,10s/root/toor/g' test.txt #1,10是範圍,後面跟s是替換,/root/替換成/toor/,g表示全局替換。 toor:x:0:0:toor:/toor:/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
上述中的參數s就表示替換的動做,參數g表示本行全局替換,若是不加g只會替換本行出現的第一個。spa
第一段和最後一段替換位置code
[root@localhost sed]# head test.txt | sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:1/' /bin/bash:x:0:0:root:/root:1 /sbin/nologin:x:1:1:bin:/bin:1 /sbin/nologin:x:2:2:daemon:/sbin:1 /sbin/nologin:x:3:4:adm:/var/adm:1 /sbin/nologin:x:4:7:lp:/var/spool/lpd:1 /bin/sync:x:5:0:sync:/sbin:1 /sbin/shutdown:x:6:0:shutdown:/sbin:1 /sbin/halt:x:7:0:halt:/sbin:1 /sbin/nologin:x:8:12:mail:/var/spool/mail:1 /sbin/nologin:x:11:0:operator:/root:1
小括號在sed中屬於特殊符號,必須在前面加轉義字符\,替換時則攜程相似\一、\2或\3的形式,上列中()把想要替換的字符打包成了一個總體,有這個轉義字符\,-r選項讓這個表達式更加清晰了。文檔
[root@localhost sed]# head test.txt |sed 's/[a-zA-Z]//g' #這裏括號裏的是範圍 ::0:0::/:// ::1:1::/:// ::2:2::/:// ::3:4:://:// ::4:7::///:// ::5:0::/:// ::6:0::/:// ::7:0::/:// ::8:12::///:// ::11:0::/://
[root@localhost sed]# head test.txt | sed -r 's/(.*)/aaa:&/' #&表示星號前全部 aaa:root:x:0:0:root:/root:/bin/bash aaa:bin:x:1:1:bin:/bin:/sbin/nologin aaa:daemon:x:2:2:daemon:/sbin:/sbin/nologin aaa:adm:x:3:4:adm:/var/adm:/sbin/nologin aaa:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin aaa:sync:x:5:0:sync:/sbin:/bin/sync aaa:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown aaa:halt:x:7:0:halt:/sbin:/sbin/halt aaa:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin aaa:operator:x:11:0:operator:/root:/sbin/nologin