sed练习题-01

发布于 2018-01-30  530 次阅读


把/etc/passwd 复制到/root/test.txt,用sed打印所有行
[root@am-01:~#] cp /etc/passwd /root/test.txt
[root@am-01:~#] sed -n '1,$'p test.txt
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
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
aming:x:1000:1000::/home/aming:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1005::/home/user3:/bin/bash
user4:x:1004:1005::/home/aming111:/sbin/nologin
user5:x:1005:1006::/home/user5:/bin/bash
user6:x:1007:1007::/home/user6:/bin/bash
打印test.txt的3到10行
[root@am-01:~#] sed -n '3,10'p test.txt
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
打印test.txt 中包含 'root' 的行
[root@am-01:~#] sed -n '/root/'p test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
删除test.txt 的15行以及以后所有行
[root@am-01:~#] sed '15,$'d test.txt
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
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
删除test.txt中包含 'bash' 的行
[root@am-01:~#] sed '/bash/'d 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
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
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
user4:x:1004:1005::/home/aming111:/sbin/nologin
替换test.txt 中 'root' 为 'toor'
[root@am-01:~#] sed -n 's/root/toor/g'p test.txt
toor:x:0:0:toor:/toor:/bin/bash
operator:x:11:0:operator:/toor:/sbin/nologin
替换test.txt中 '/sbin/nologin' 为 '/bin/login'
[root@am-01:~#] sed -n 's@/sbin/nologin@/bin/login@g'p test.txt
bin:x:1:1:bin:/bin:/bin/login
daemon:x:2:2:daemon:/sbin:/bin/login
adm:x:3:4:adm:/var/adm:/bin/login
lp:x:4:7:lp:/var/spool/lpd:/bin/login
mail:x:8:12:mail:/var/spool/mail:/bin/login
operator:x:11:0:operator:/root:/bin/login
games:x:12:100:games:/usr/games:/bin/login
ftp:x:14:50:FTP User:/var/ftp:/bin/login
nobody:x:99:99:Nobody:/:/bin/login
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/bin/login
dbus:x:81:81:System message bus:/:/bin/login
polkitd:x:999:998:User for polkitd:/:/bin/login
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/bin/login
postfix:x:89:89::/var/spool/postfix:/bin/login
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/bin/login
ntp:x:38:38::/etc/ntp:/bin/login
user4:x:1004:1005::/home/aming111:/bin/login
删除test.txt中5到10行中所有的数字
[root@am-01:~#] sed -n '5,10'p test.txt | sed 's@[0-9]@@g'
lp:x:::lp:/var/spool/lpd:/sbin/nologin
sync:x:::sync:/sbin:/bin/sync
shutdown:x:::shutdown:/sbin:/sbin/shutdown
halt:x:::halt:/sbin:/sbin/halt
mail:x:::mail:/var/spool/mail:/sbin/nologin
operator:x:::operator:/root:/sbin/nologin
删除test.txt中所有特殊字符(除了数字以及大小写字母)
[root@am-01:~#] sed -n 's@[^0-9a-zA-Z]@@g'p test.txt
rootx00rootrootbinbash
binx11binbinsbinnologin
daemonx22daemonsbinsbinnologin
admx34admvaradmsbinnologin
lpx47lpvarspoollpdsbinnologin
syncx50syncsbinbinsync
shutdownx60shutdownsbinsbinshutdown
haltx70haltsbinsbinhalt
mailx812mailvarspoolmailsbinnologin
operatorx110operatorrootsbinnologin
gamesx12100gamesusrgamessbinnologin
ftpx1450FTPUservarftpsbinnologin
nobodyx9999Nobodysbinnologin
avahiautoipdx170170AvahiIPv4LLStackvarlibavahiautoipdsbinnologin
dbusx8181Systemmessagebussbinnologin
polkitdx999998Userforpolkitdsbinnologin
tssx5959Accountusedbythetrouserspackagetosandboxthetcsddaemondevnullsbinnologin
postfixx8989varspoolpostfixsbinnologin
sshdx7474PrivilegeseparatedSSHvaremptysshdsbinnologin
amingx10001000homeamingbinbash
ntpx3838etcntpsbinnologin
user1x10011001homeuser1binbash
user2x10021002homeuser2binbash
user3x10031005homeuser3binbash
user4x10041005homeaming111sbinnologin
user5x10051006homeuser5binbash
user6x10071007homeuser6binbash
把test.txt中第一个单词和最后一个单词调换位置
[root@am-01:~#] sed 's/\(^[a-zA-Z][a-zA-Z]*\)\([^a-zA-Z].*\)\([^a-zA-Z]\)\([a-zA-Z][a-zA-Z]*$\)/\4\2\3\1/' test.txt
bash:x:0:0:root:/root:/bin/root
nologin:x:1:1:bin:/bin:/sbin/bin
nologin:x:2:2:daemon:/sbin:/sbin/daemon
nologin:x:3:4:adm:/var/adm:/sbin/adm
nologin:x:4:7:lp:/var/spool/lpd:/sbin/lp
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:8:12:mail:/var/spool/mail:/sbin/mail
nologin:x:11:0:operator:/root:/sbin/operator
nologin:x:12:100:games:/usr/games:/sbin/games
nologin:x:14:50:FTP User:/var/ftp:/sbin/ftp
nologin:x:99:99:Nobody:/:/sbin/nobody
nologin-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/avahi
nologin:x:81:81:System message bus:/:/sbin/dbus
nologin:x:999:998:User for polkitd:/:/sbin/polkitd
nologin:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/tss
nologin:x:89:89::/var/spool/postfix:/sbin/postfix
nologin:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/sshd
bash:x:1000:1000::/home/aming:/bin/aming
nologin:x:38:38::/etc/ntp:/sbin/ntp
bash1:x:1001:1001::/home/user1:/bin/user
bash2:x:1002:1002::/home/user2:/bin/user
bash3:x:1003:1005::/home/user3:/bin/user
nologin4:x:1004:1005::/home/aming111:/sbin/user
bash5:x:1005:1006::/home/user5:/bin/user
bash6:x:1007:1007::/home/user6:/bin/user
把test.txt中出现的第一个数字和最后一个单词替换位置
[root@am-01:~#] sed 's#\([^0-9][^0-9]*\)\([0-9][0-9]*\)\([^0-9].*\)\([^a-zA-Z]\)\([a-zA-Z][a-zA-Z]*$\)#\1\5\3\4\2#' test.txt
root:x:bash:0:root:/root:/bin/0
bin:x:nologin:1:bin:/bin:/sbin/1
daemon:x:nologin:2:daemon:/sbin:/sbin/2
adm:x:nologin:4:adm:/var/adm:/sbin/3
lp:x:nologin:7:lp:/var/spool/lpd:/sbin/4
sync:x:sync:0:sync:/sbin:/bin/5
shutdown:x:shutdown:0:shutdown:/sbin:/sbin/6
halt:x:halt:0:halt:/sbin:/sbin/7
mail:x:nologin:12:mail:/var/spool/mail:/sbin/8
operator:x:nologin:0:operator:/root:/sbin/11
games:x:nologin:100:games:/usr/games:/sbin/12
ftp:x:nologin:50:FTP User:/var/ftp:/sbin/14
nobody:x:nologin:99:Nobody:/:/sbin/99
avahi-autoipd:x:nologin:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/170
dbus:x:nologin:81:System message bus:/:/sbin/81
polkitd:x:nologin:998:User for polkitd:/:/sbin/999
tss:x:nologin:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/59
postfix:x:nologin:89::/var/spool/postfix:/sbin/89
sshd:x:nologin:74:Privilege-separated SSH:/var/empty/sshd:/sbin/74
aming:x:bash:1000::/home/aming:/bin/1000
ntp:x:nologin:38::/etc/ntp:/sbin/38
userbash:x:1001:1001::/home/user1:/bin/1
userbash:x:1002:1002::/home/user2:/bin/2
userbash:x:1003:1005::/home/user3:/bin/3
usernologin:x:1004:1005::/home/aming111:/sbin/4
userbash:x:1005:1006::/home/user5:/bin/5
userbash:x:1007:1007::/home/user6:/bin/6
把test.txt 中第一个数字移动到行末尾
[root@am-01:~#] sed 's#\([^0-9][^0-9]*\)\([0-9][0-9]*\)\([^0-9].*$\)#\1\3\2#' test.txt
root:x::0:root:/root:/bin/bash0
bin:x::1:bin:/bin:/sbin/nologin1
daemon:x::2:daemon:/sbin:/sbin/nologin2
adm:x::4:adm:/var/adm:/sbin/nologin3
lp:x::7:lp:/var/spool/lpd:/sbin/nologin4
sync:x::0:sync:/sbin:/bin/sync5
shutdown:x::0:shutdown:/sbin:/sbin/shutdown6
halt:x::0:halt:/sbin:/sbin/halt7
mail:x::12:mail:/var/spool/mail:/sbin/nologin8
operator:x::0:operator:/root:/sbin/nologin11
games:x::100:games:/usr/games:/sbin/nologin12
ftp:x::50:FTP User:/var/ftp:/sbin/nologin14
nobody:x::99:Nobody:/:/sbin/nologin99
avahi-autoipd:x::170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin170
dbus:x::81:System message bus:/:/sbin/nologin81
polkitd:x::998:User for polkitd:/:/sbin/nologin999
tss:x::59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin59
postfix:x::89::/var/spool/postfix:/sbin/nologin89
sshd:x::74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin74
aming:x::1000::/home/aming:/bin/bash1000
ntp:x::38::/etc/ntp:/sbin/nologin38
user:x:1001:1001::/home/user1:/bin/bash1
user:x:1002:1002::/home/user2:/bin/bash2
user:x:1003:1005::/home/user3:/bin/bash3
user:x:1004:1005::/home/aming111:/sbin/nologin4
user:x:1005:1006::/home/user5:/bin/bash5
user:x:1007:1007::/home/user6:/bin/bash6
在test.txt 20行到末行最前面加 'aaa:'
[root@am-01:~#] sed '20,$s/^.*$/aaa:&/' test.txt
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
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
aaa:aming:x:1000:1000::/home/aming:/bin/bash
aaa:ntp:x:38:38::/etc/ntp:/sbin/nologin
aaa:user1:x:1001:1001::/home/user1:/bin/bash
aaa:user2:x:1002:1002::/home/user2:/bin/bash
aaa:user3:x:1003:1005::/home/user3:/bin/bash
aaa:user4:x:1004:1005::/home/aming111:/sbin/nologin
aaa:user5:x:1005:1006::/home/user5:/bin/bash
aaa:user6:x:1007:1007::/home/user6:/bin/bash