sed使用場景:有時候須要修改配置文件或者腳本時,例如刪除或置換某些關鍵詞,就須要用到sed。正則表達式
語法:
bash
sed [選項] '地址定界和命令' file……app
工做機制:每次讀取一行文本至「模式空間(pattern space)」中,在模式空間中完成處理;將處理結果輸出至標準輸出設備;ide
選項:
spa
-n: 靜默模式,不顯示沒有匹配到的內容3d
-i:直接修改源文件regexp
-e:script -e script:能夠同時執行多個操做ip
-f:/path/to/sed_script :從指定文件中讀取腳本並運行rem
-r:表示使用正則表達式字符串
地址定界方式:
#:指定行
$:最後一行
/regexp/:任何能被正則表達式所匹配到的行
/regexp/|:匹配時忽略字符大小寫
starLine(起始行),endLine(結束行)
#,/regexp/:從#行開始,到第一次被/regexp/所匹配到的行結束,中間的全部行;
#,#:#行開始,#行結束
/regexp1/,/regexp2/:從第一次被/regexp1/匹配到的行開始,到第一次被/regexp2/匹配到的行結束,中間的全部行;
#,+n:從#行開始,一直到向下的n行;
first~step:指定起始行,以及步長;
命令:
d:刪除符合條件的行
=:顯示行號;
p:顯示符合條件的行
c \test:用text替換匹配到的行
a:\test:在指定的行後面追加新行,內容爲test
i:\test:在指定的行前面添加新行,內容爲test
r file:將指定的文件的內容添加至符合條件的行處
w file:將地址指定的範圍內的行另存至指定的文件中
s/pattern/string/: 查找並替換,默認只替換每行中第一次被模式匹配到的字符串
g:全局替換
i:忽略字符大小寫
s/// s### s@@@ 若是查找的內容出現和分割符一致能夠更換分隔符,只要三個分隔符同樣就能夠
操做:
演示的文件:下面的一切操做都是根據此文件進行操做。
[root@localhost ~]# cat fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
d:刪除符合條件的行
刪除1行到第3行
[root@localhost ~]# sed '1,3d' fstab # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/orblkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
刪除最後一行
[root@localhost ~]# sed '$d' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0
刪除包含default字符的行
[root@localhost ~]# sed '/default/d' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # devpts /dev/pts devpts gid=5,mode=620 0 0
=:顯示行號
顯示行號,比較坑的一點就是行號在行的上方,若是想在行前方能夠使用cat -n
[root@localhost ~]# sed '=' fstab 1 # /etc/fstab 2 # Created by anaconda on Thu Apr 23 09:07:40 2015 3 # 4 # Accessible filesystems, by reference, aremaintained under '/dev/disk' 5 # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info 6 # 7 /dev/mapper/vg0-root / ext4 defaults 1 1 8 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 9 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 10 /dev/mapper/vg0-var /var ext4 defaults 1 2 11 /dev/mapper/vg0-swap swap swap defaults 0 0 12 tmpfs /dev/shm tmpfs defaults 0 0 13 devpts /dev/pts devpts gid=5,mode=620 0 0 14 sysfs /sys sysfs defaults 0 0 15 proc /proc proc defaults 0 0
只給#號開頭的行顯示行號
[root@localhost ~]# sed '/^#/=' fstab 1 # /etc/fstab 2 # Created by anaconda on Thu Apr 23 09:07:40 2015 3 # 4 # Accessible filesystems, by reference, aremaintained under '/dev/disk' 5 # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info 6 # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
p:顯示符合條件的行
顯示最後一行;可是有一個缺點就是不去重,因此最後一行顯示了兩次
[root@localhost ~]# sed '$p' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 proc /proc proc defaults 0 0
[root@localhost ~]# sed -n'$p' fstab UUID=1a829b6d-763f-44cd-8544-f5fc4a480d05 swap swap defaults 0 0
c \test:用text替換匹配到的行
地址定界替換和模式匹配替換的區別:
地址定界:全部行替換爲一行
模式匹配:全部行分別替換
地址定界:將第5行,到第7行替換爲new text
[root@localhost ~]# sed -n '5,7c \new test' fstab new test
模式匹配:將第5行,到第7行分別替換爲new text
[root@localhost ~]# sed '/^#/c \new test' fstab new test new test new test new test new test new test /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
a:\test:在指定的行後面追加新行,內容爲test
在第一行後面添加新行
[root@localhost ~]# sed '1a \新添加的行' fstab # /etc/fstab 新添加的行 # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
添加多行,在行尾處使用\n換號便可
[root@localhost ~]# sed '1a \添加的第一行\n第二行\n第三行' fstab # /etc/fstab 添加的第一行 第二行 第三行 # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
i:\test:在指定的行前面添加新行,內容爲test
[root@localhost ~]# sed '1i \新添加的行' fstab 新添加的行 # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db /boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
r file:將指定的文件的內容添加至符合條件的行處
指定的文件
[root@localhost ~]# cat /etc/redhat-release CentOS release 6.6 (Final)
把指定的文件添加到第1行
[root@localhost ~]# sed '1r' /etc/redhat-releasefstab CentOS release 6.6 (Final) # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
w file:將地址指定的範圍內的行另存至指定的文件中
將第一行到第五行,保存到test.txt文件中
[root@localhost ~]# sed '1,5w test.txt' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
查看保存的文件
[root@localhost ~]# cat test.txt # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, are maintainedunder '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info
刪除行首開頭的#號
[root@localhost ~]# sed 's@^#@@' fstab /etc/fstab Created byanaconda on Thu Apr 23 09:07:40 2015 Accessiblefilesystems, by reference, are maintained under '/dev/disk' See manpages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
練習:(1) 刪除/boot/grub/grub.conf文件中全部行的行首的空白字符; sed 's/^[[:space:]]\+//' /boot/grub/grub.conf(2) 刪除/etc/fstab文件中全部以#開頭,後跟至少一個空白字符的行的行首的#和空白字符; sed 's/^#[[:space:]]\+//' /etc/fstab(3) 把/etc/fstab文件的奇數行另存爲/tmp/fstab.3; sed '1~2w /tmp/fstab.3' /etc/fstab(4) echo一個文件路徑給sed命令,取出其基名;進一步地,取出其路徑名; 取基名:echo "/etc/sysconfig/network-scripts/" | sed 's@^.*/\([^/]\+\)/\?$@\1@' 取路徑名:echo "/etc/sysconfig/network-scripts/" | sed 's@[^/]\+/\?$@@'