【CentOS 7筆記37】,sed命令#

shallow丿ovephp


[root@localhost ~]# mkdir sed
[root@localhost ~]# cd sed/
[root@localhost sed]# cp ../grep/inittab test.txt

sed雖然比grep麻煩一點,但sed有一個強大的功能就是查找替換mysql

'/str/'p

[root@localhost sed]# sed '/root/'p test.txt 
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
root:x:0:0:root:/root:/bin/bash
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/NOLogin
operator:x:11:0:operator:/root:/sbin/nologin
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
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

這裏匹配到root,而且打印了兩次同時其餘無關的信息也打印了出來,-n能夠不現實其餘無關的sql

-n '/str/'p

[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

-r脫義

[root@localhost sed]# sed -n '/ro\+t/'p test.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost sed]# sed -n '/ro+t/'p test.txt 
[root@localhost sed]# sed -nr '/ro+t/'p test.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

相似於grep -Eapache

一樣支持正則

[root@localhost sed]# sed -n '/r.t/'p test.txt 
# multi-user.target: analogous to runlevel 3
operator:x:11:0:operator:/root:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

[root@localhost sed]# sed -nr '/o{2}/'p test.txt 
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/NOLogin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[root@localhost sed]# sed -nr '/root|bus/'p test.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin

打印指定的行'num'

[root@localhost sed]# sed -n '10'p test.txt 
# graphical.target: analogous to runlevel 5

打印範圍行'num,num'

[root@localhost sed]# sed -n '2,10'p test.txt 
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5

打印某行到末行的內容'num,$'

[root@localhost sed]# sed -n '30,$'p test.txt 
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

同時打印-e

[root@localhost sed]# sed -e '1'p -e '/bus/'p -n test.txt 
# inittab is no longer used when using systemd.
dbus:x:81:81:System message bus:/:/sbin/nologin

不區分大小寫'/user/'Ip

[root@localhost sed]# sed -n '/user/'p test.txt 
# multi-user.target: analogous to runlevel 3
[root@localhost sed]# sed -n '/user/'Ip test.txt 
# multi-user.target: analogous to runlevel 3
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin

過濾刪除行'num,num'd

[root@localhost sed]# sed '1,35'd test.txt 
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost sed]# wc -l test.txt 
40 test.txt

並無直接刪除bash

-i刪除

[root@localhost sed]# cp test.txt test.txt.bak
[root@localhost sed]# sed -i '1,35'd test.txt 
[root@localhost sed]# wc -l test.txt 
5 test.txt
[root@localhost sed]# sed -i '/ssh/'d test.txt 
[root@localhost sed]# cat test.txt
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

查找替換

[root@localhost sed]# cp test.txt.bak test.txt
cp: overwrite ‘test.txt’? y
[root@localhost sed]# sed '1,10s/#/@/g' test.txt
@ inittab is no longer used when using systemd.
@
@ ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
@
@ Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
@
@ systemd uses 'targets' instead of runlevels. By default, there are two main targets:
@
@ multi-user.target: analogous to runlevel 3
@ graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/NOLogin
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
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

使用方法與vi命令中的查找方式相似ssh

使用貪婪匹配替換

[root@localhost sed]# tail test.txt | sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'
/sbin/nologin:x:81:81:System message bus:/:dbus
/sbin/nologin:x:999:998:User for polkitd:/:polkitd
/sbin/nologin:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:avahi
/sbin/nologin:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:avahi-autoipd
/sbin/nologin:x:89:89::/var/spool/postfix:postfix
/sbin/nologin:x:74:74:Privilege-separated SSH:/var/empty/sshd:sshd
/bin/bash:x:1000:1000::/home/mysql:mysql
/sbin/nologin:x:192:192:systemd Network Management:/:systemd-network
/sbin/nologin:x:1001:1001::/home/php-fpm:php-fpm
/sbin/nologin:x:48:48:Apache:/usr/share/httpd:apache

使用\、@、#區分

[root@localhost sed]# tail test.txt | sed -r 's/\/sbin\/nologin/123/g'
dbus:x:81:81:System message bus:/:123
polkitd:x:999:998:User for polkitd:/:123
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:123
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:123
postfix:x:89:89::/var/spool/postfix:123
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:123
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:123
php-fpm:x:1001:1001::/home/php-fpm:123
apache:x:48:48:Apache:/usr/share/httpd:123

刪除英文字母

[root@localhost sed]# tail test.txt | sed -r 's/[a-zA-Z]//g'
::81:81:  :/://
::999:998:  :/://
::70:70: /- :///-://
-::170:170: 4 :///-://
::89:89::///://
::74:74:- :///://
::1000:1000:://://
-::192:192:  :/://
-::1001:1001:://-://
::48:48::///://

替換成空php-fpm

替換前面字符

[root@localhost sed]# tail test.txt | sed -r 's/(.*)/aaa:&/'
aaa:dbus:x:81:81:System message bus:/:/sbin/nologin
aaa:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
aaa:avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
aaa:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
aaa:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
aaa:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
aaa:mysql:x:1000:1000::/home/mysql:/bin/bash
aaa:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
aaa:php-fpm:x:1001:1001::/home/php-fpm:/sbin/nologin
aaa:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

(.*)表示一整行post

相關文章
相關標籤/搜索