2021-05-12 14:32:11
Linux sed 命令操作詳解
sed命令
-n 靜默模式,不輸出模式空間中的內容
編輯命令:
d:刪除
地址定界:
startline,endline
1,3
/part1/,/part2/ 匹配範圍之間
/part/ 匹配到所匹配的字元
-n:靜默模式,只顯示匹配字串或者行.
a 在當前行下面插入文字。
i 在當前行上面插入文字。
r:在匹配航之後引入另一個檔案內容.
w:將匹配到的行輸出到另一個檔案.
p:將匹配的行列印出來(也會輸出原檔案的內容),pn只顯示列印的行.
[[:upper:]] 匹配以大寫字母開頭的行.
=:顯示匹配行的行號,-n和=同時使用,只輸出匹配行的行號.
[^]:匹配一個不在指定範圍內的字元.
^ 匹配行開始,如:/^sed/匹配所有以sed開頭的行。
$ 匹配行結束,如:/sed$/匹配所有以sed結尾的行。
. 匹配一個非換行符的任意字元,如:/s.d/匹配s後接一個任意字元,最後是d。 * 匹配0個或多個字元,如:/*sed/匹配所有模板是一個或多個空格後緊跟sed的行。
-f /path/to/sed_script_file :指定sed指令碼檔案.
-e 'script' -e 'script':指定多個編輯命令
正規表示式:
基本的:
字元:. [] [^]
次數:* ? {}
錨定:^ $ < >
分組:()
參照:1,2,...
擴充套件:
字元:. [] [^]
[[:lower:]]:匹配所有的小寫
[[:punct:]]:匹配所有的標點符號
[[:alnum:]]:匹配所有的數位
次數:
* ? + {m,n}
錨定:
^ $ > <
或:
a|b
分組:
()
參照:
1,2
sed '地址定界s@查詢條件@替換文字@' :在指定的定界內查詢匹配到的條件做替換
修飾符:
g:global,全域性替換
i:ignore-case不區分大小寫
1)、[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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 '1,3d' /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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 引數
sed -n '1,3d' /etc/fstab 不會輸出任何東西,靜默模式
2)、刪除所有以#號開頭的行:
[root@localhost ~]
# sed '/^#/d' /etc/fstab
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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
3)、刪除所有以/ 開頭的行:
[root@localhost ~]# sed '/^//d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
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
4)、刪除從第2行開始,到第一次以“/”開頭的行:
[root@localhost ~]# sed '2,/^//d' /etc/fstab
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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
5)、刪除第一個#號開頭的行到第一個"/",開頭的行:
[root@localhost ~]# sed '/^#/,/^//d' /etc/fstab
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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 列印引數
6)、列印第一個#號開頭的行到第一個"/",開頭的行,如果不加-n引數靜默模式,會輸出重複匹配的內容:
[root@localhost ~]# sed -n '/^#/,/^//p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
不加-n引數結果:
[root@localhost ~]# sed '/^#/,/^//p' /etc/fstab
#
#
# /etc/fstab
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
#
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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和i引數:
a:在匹配的行之後新增內容
i: 在匹配的行之前新增內容
#再#號開頭的行之後新增hello world
# sed '/^#/a hello world' /etc/fstab
#
hello world
# /etc/fstab
hello world
# Created by anaconda on Fri Feb 24 02:20:51 2017
hello world
#
hello world
# Accessible filesystems, by reference, are maintained under '/dev/disk'
hello world
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
hello world
#
hello world
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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
#在匹配的行之前新增hello world欄位。
# sed '/^//i hello world' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
hello world
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
hello world
/dev/mapper/VolGroup-lv_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
匹配在大寫字母開頭的行之後新增二行,第一行hello,第二行second。注釋:匹配大寫字母以[[:upper:]]
# sed '/^[[:upper:]]/a hello nsecond' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
hello
second
/dev/mapper/VolGroup-lv_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 命令 /path/to/somefile :在指定位置把另一個檔案的內容插入進來。
# sed '/^UUID/r /etc/issue' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
CentOS release 6.5 (Final)
Kernel r on an m
/dev/mapper/VolGroup-lv_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:/path/to/somefile:將符合條件的所有行儲存至指定檔案
# sed '/^UUID/w /tmp/uuid.txt' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root
/ ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
/dev/mapper/VolGroup-lv_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
檢視
/tmp/uuid
.txt的檔案內容。
[root@localhost ~]
# cat /tmp/uuid.txt
UUID=2020ea71-49cf-488e-bd6c-de72a001082b
/boot
ext4 defaults 1 2
=: 只顯示匹配行的行號
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2017-05/144214p2.htm
相關文章