處理文本的工具sed
Stream EDitor, 行編輯器 sed 是一種流編輯器,它一次處理一行內容。處理時,把 當前處理的行存儲在臨時緩沖區中,稱為“模式空間”( pattern space ),接著用sed 命令處理緩沖區中的內容 ,處理完成后,把緩沖區的內容送往屏幕。接著處理下一 行,這樣不斷重復,直到文件末尾。文件內容并沒有改變 ,除非你使用重定向存儲輸出。Sed 主要用來自動編輯一 個或多個文件, 簡化對文件的反復操作,編寫準換程序等。
sed
用法: sed [option]... 'script' inputfile... 常用選項: -n :不輸出模式空間內容的自動打印 -e: 多點編輯 [root@localhost ~]# sed -e '2,10d' -e '/rpc/,/rooter/d' f1 root:x:0:0:root:/root:/bin/bash games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:997:995:User for polkitd:/:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin colord:x:996:994:User for colord:/var/lib/colord:/sbin/nologin libstoragemgmt:x:995:992:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin -f / PATH/TO/SCRIPT_FILE : 從指定文件中讀取編輯腳本 [root@localhost ~]# cat f2 {lasfhakshfkasdlajsflda;fjlaksdfjlkas}if asdfasljfdalsjdfalkjfdlajf skdjafksafhakhsfakhfahflahfasfjk skdjafksafhakhsfakhfahflahfasfjk asdkfalksdfjal;sdjfklasdf skdjafksafhakhsfakhfahflahfasfjk [root@localhost ~]# sed -nf f1 f2 asdfasljfdalsjdfalkjfdlajf -r: 支持使用擴展正則表達式 [root@localhost ~]# echo "/etc/functions" | sed -r 's@/.*/([^/]+/?$)@\1@' functions -i: 原處編輯 -i.bak 在對file操作之前先對file進行備份,備份的文件名是file.bak script:腳本 地址+命令 地址定界: (1) 不給地址:對全文進行處理 (2) 單地址: #: 指定的行 /pattern/ :被此處模式所能夠匹配到的每一行 [root@localhost ~]# cat /etc/fstab | sed -n '/^U/p' UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 (3) 地址范圍: #,# [root@localhost ~]# sed -n '9,12p' /etc/fstab UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 #,+# [root@localhost ~]# sed -n '3,+4p' /etc/fstab # /etc/fstab # Created by anaconda on Mon Jul 25 12:04:17 2016 # # 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 /pat1/,/pat2/ [root@localhost ~]# sed -n '/^root\b/,/^s/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 sync:x:5:0:sync:/sbin:/bin/sync #,/pat1/ [root@localhost ~]# sed -n '1,/^s/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 sync:x:5:0:sync:/sbin:/bin/sync (4) ~ :步進 1~2 奇數行 2~2 偶數行 編輯命令: d: 刪除模式空間匹配的行 [root@localhost ~]# cat f1 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Mon Jul 25 12:04:17 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 10 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 11 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 12 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 [root@localhost ~]# sed '3d' f1 1 2 # 4 # Created by anaconda on Mon Jul 25 12:04:17 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 10 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 11 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 12 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 p: 顯示模式空間中的內容 [root@localhost ~]# sed -n '3p' f1 3 # /etc/fstab a \text :在行后面追加文本;支持使用\n 實現多行追加 [root@localhost ~]# sed '3a\skdfjskdfjksdf' f1 1 2 # 3 # /etc/fstab skdfjskdfjksdf 4 # Created by anaconda on Mon Jul 25 12:04:17 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 10 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 11 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 12 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 i \text :在行前面插入文本;支持使用\n 實現多行插入 [root@localhost ~]# sed '3i\123456' f1 1 2 # 123456 3 # /etc/fstab 4 # Created by anaconda on Mon Jul 25 12:04:17 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 10 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 11 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 12 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 c \text :替換行為單行或多行文本 [root@localhost ~]# sed '3c\123456abcdefg' f1 1 2 # 123456abcdefg 4 # Created by anaconda on Mon Jul 25 12:04:17 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 10 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 11 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 12 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 w /path/to/somefile: 保存模式匹配的行至指定文件 r /path/from/somefile :讀取指定文件的文本至模式空 間中匹配到的行后 =: 為模式空間中的行打印行號 [root@localhost ~]# sed = fstab 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Mon Jul 25 12:04:17 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=f4406f6a-e495-45a0-a85e-3b059c0d3130 / xfs defaults 0 0 10 UUID=7c25120e-2371-413d-b584-fdd695b96702 /boot xfs defaults 0 0 11 UUID=19470291-724c-4f01-b6e1-7109ad22be1b /usr xfs defaults 0 0 12 UUID=c3460309-9e8c-4037-8684-4c6bdcabbacb swap swap defaults 0 0 [root@localhost ~]# !: 模式空間中匹配行取反處理
-
sed搜索替換 s/// :查找替換, 支持使用其它分隔符,s@@@ ,s### [root@localhost ~]# sed 's/root/god/' f3 god god god root root 替換標記: g: 行內全局替換 [root@localhost ~]# sed 's/root/god/g' f3 god god god god god p: 顯示替換成功的行 w /PATH/TO/SOMEFILE :將替換成功的行保存至指定的文件中
-
sed高級編輯命令
高級編輯命令: h: 把模式空間中的內容覆蓋至保持空間中 H :把模式空間中的內容追加至保持空間中 g: 從保持空間取出數據覆蓋至模式空間 G :從保持空間取出內容追加至模式空間 x: 把模式空間中的內容與保持空間中的內容進行互換 n: 讀取匹配到的行的下一行 覆蓋 至模式空間 N :追加匹配到的行的下一行至模式空間 d: 刪除模式空間中的行 D :刪除 當前模式空間開端至\n 的內容(不在傳至標準輸出),放棄之后的命令,但是對剩余模式空間重新執行sed nl file 加行號
原創文章,作者:dxkboke,如若轉載,請注明出處:http://www.www58058.com/32521