六週第二次課mysql
9.4/9.5 sed正則表達式
grep只能實現查找功能,而不能把查找的內容替換。vim能夠查找和替換,但只限於在文本內部操做,而不能輸出到屏幕上。sed和awk都是流式編輯器,是針對文檔的行來操做的。sql
sed和grep相比,也能實現grep的功能,可是很麻煩,也沒有顏色顯示。可是,sed有本身更加牛逼的地方,它的強項在於替換,替換一些指定的字符。apache
sed是一個很好的文件處理工具,自己是一個管道命令,主要是以行爲單位進行處理,能夠將數據行進行替換、刪除、新增、選取等特定工做,下面先了解一下sed的用法
sed命令行格式爲:vim
sed [-nefri] ‘command’ 輸入文本 api
經常使用選項:tomcat
-n∶使用安靜(silent)模式。-n選項的做用是隻顯示咱們要打印的行,可有可無的內容不顯示。bash
[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
[root@localhost sed]# ssh
[root@localhost sed]# sed -n '/r.t/'p test.txt
operator:x:11:0:operator:/root:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost sed]# sed -n '/r*t/'p test.txt
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
[root@localhost sed]# tcp
-r∶支持不用脫義符號
sed是支持使用+的,可是要使用脫義符號,若是不想用脫義符號,能夠加-r選項。
[root@localhost sed]# sed -n '/o\+t/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
[root@localhost sed]# sed -nr '/o+t/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
[root@localhost sed]#
下面匹配2次o。
[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
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@localhost sed]#
使用|或者符號
[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
[root@localhost sed]#
直接打印第2行
[root@localhost sed]# sed -n '2'p test.txt
bin:x:1:1:bin:/bin:/sbin/NOlogin
打印2到5行
[root@localhost sed]# sed -n '2,5'p test.txt
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
[root@localhost sed]#
打印45到最後一行
[root@localhost sed]# sed -n '45,$'p test.txt
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost sed]#
打印出所有內容,使用命令:
[root@localhost sed]# sed -n '1,$'p test.txt
sed命令加上-e選項能夠實現多個行爲,這些行爲會依次執行,可能會出現重複顯示。
[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
[root@localhost sed]#
[root@localhost sed]# sed -e '1'p -e '/root/'p -n test.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost sed]#
[root@localhost sed]# sed -e '1'p -e '/root/'p -e '/oo*/'p -n test.txt
root:x:0:0:root:/root:/bin/bash
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:386:384::/var/lib/chrony:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
sssd:x:384:381:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost sed]#
-Ip:不區分大小寫
[root@localhost sed]# sed -n '/bus/'Ip test.txt
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@localhost sed]#
sed命令加上d:刪除指定行。參數d表示刪除的動做,僅僅是在顯示器屏幕上並不顯示這些行而已,文檔不受影響。
[root@localhost sed]# sed '1,50'd test.txt
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost sed]#
[root@localhost sed]# wc -l test.txt
61 test.txt
[root@localhost sed]#
sed命令加上-i:刪除指定行
[root@localhost sed]# cp test.txt test.txt.bak
[root@localhost sed]# sed -i '1,50'd test.txt
[root@localhost sed]# wc -l test.txt
11 test.txt
[root@localhost sed]#
[root@localhost sed]# cat test.txt
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost sed]# sed -i '/aming/'d test.txt
[root@localhost sed]# cat test.txt
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost sed]#
替換字符或字符串
參數s表示替換的動做,參數g表示全局替換,若是不加g則只替換本行出現的第一個,用戶和vim的替換同樣
[root@localhost sed]# sed '1,10s/root/toor/g' test.txt
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
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:/toor:/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
ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:386:384::/var/lib/chrony:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin
sssd:x:384:381:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash
aming:x:1001:1001::/home/aming:/bin/bash
user1:x:1002:1002::/home/user1:/bin/bash
[root@localhost sed]#
要替換的字符串是可使用正則表達式的,如.、*、+等等。
[root@localhost sed]# sed -r '1,10s/ro+/r/g' test.txt | head
rt:x:0:0:rt:/rt:/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:/rt:/sbin/nologin
[root@localhost sed]#
():小括號在sed中屬於特殊符號,用()把想要替換的字符打包成了一個總體,替換時則寫成相似\一、\2或\3的形式。
(.*)是貪婪匹配,會一直匹配到最後一個:
下面替換test.txt前10行的第一段和最後一段。由於每一行都是以:分割的,非冒號字符匹配第一段,貪婪匹配匹配中間全部段,非冒號字符匹配最後一段。
[root@localhost sed]# head test.txt | sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'
/bin/bash:x:0:0:root:/root:root
/sbin/NOlogin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
[root@localhost sed]#
除了可使用 / 做爲分隔符外,咱們還可使用其餘特殊符號做爲分隔符,如#和@
[root@localhost sed]# head test.txt | sed 's@/sbin/nologin@123@g'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/NOlogin
daemon:x:2:2:daemon:/sbin:123
adm:x:3:4:adm:/var/adm:123
lp:x:4:7:lp:/var/spool/lpd:123
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:123
operator:x:11:0:operator:/root:123
[root@localhost sed]#
刪除各行的字母,刪除的意思是用空格來替換
[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]#
在全部行前面加上固定的字符串
&:匹配須要替換的內容,(.*)表示一整行。
[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
[root@localhost sed]#
友情連接:阿銘Linux