最新消息:阿啰哈,本人90后,目前单身,欢迎妹子们来撩!.(。→‿←。)

sed命令练习2

#1、定位行
```
 1 # inittab is only used by upstart for the default runlevel.
 2 #
 3 # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
 4 #
 5 # System initialization is started by /etc/init/rcS.conf
 6 #
 7 # Individual runlevels are started by /etc/init/rc.conf
 8 #
 9 # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
10 #
11 # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
12 # with configuration in /etc/sysconfig/init.
13 #
14 # For information on how to write upstart event handlers, or how
15 # upstart works, see init(5), init(8), and initctl(8).
16 #
17 # Default runlevel. The runlevels used are:
18 #   0 - halt (Do NOT set initdefault to this)
19 #   1 - Single user mode
20 #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
21 #   3 - Full multiuser mode
22 #   4 - unused
23 #   5 - X11
24 #   6 - reboot (Do NOT set initdefault to this)
25 #
26 id:3:initdefault:

```
####打印第3行
`sed -n '3p' a`
 3 # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

####打印第4行至第6行
`sed -n '4,6p' a`
 4 #
 5 # System initialization is started by /etc/init/rcS.conf
 6 #

####打印最后一行
`sed -n '$p' a`
26 id:3:initdefault:

####打印第24行至最后一行
`sed -n '24,$p' a`
24 #   6 - reboot (Do NOT set initdefault to this)
25 #
26 id:3:initdefault:

####从第20行开始,把其后续的两行也一并打印出来
`sed -n '20,+2p' a`
20 #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
21 #   3 - Full multiuser mode
22 #   4 - unused

####first~step:指定起始的位置及步长。例1,从第15行开始,以3为步长打印
`sed -n '15~3p' a`
15 # upstart works, see init(5), init(8), and initctl(8).
18 #   0 - halt (Do NOT set initdefault to this)
21 #   3 - Full multiuser mode
24 #   6 - reboot (Do NOT set initdefault to this)

####例2,从第0行开始,以2为步长打印。实际效果为打印出偶数行
`sed -n '0~2p' a`
 2 #
 4 #
 6 #
 8 #
10 #
12 # with configuration in /etc/sysconfig/init.
14 # For information on how to write upstart event handlers, or how
16 #
18 #   0 - halt (Do NOT set initdefault to this)
20 #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
22 #   4 - unused
24 #   6 - reboot (Do NOT set initdefault to this)
26 id:3:initdefault:

####例3,从第1行开始,以2为步长打印。实际效果为打印出奇数行
`sed -n '1~2p' a`
 1 # inittab is only used by upstart for the default runlevel.
 3 # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
 5 # System initialization is started by /etc/init/rcS.conf
 7 # Individual runlevels are started by /etc/init/rc.conf
 9 # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
11 # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
13 #
15 # upstart works, see init(5), init(8), and initctl(8).
17 # Default runlevel. The runlevels used are:
19 #   1 - Single user mode
21 #   3 - Full multiuser mode
23 #   5 - X11
25 #

####例4,从第0行开始,为10为步长打印。
`sed -n '0~10p' a`
10 #
20 #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)

####从第12行开始,直到下一个7的倍数行(此示例会显示12-17行)
`sed -n '12,~7p' a`
12 # with configuration in /etc/sysconfig/init.
13 #
14 # For information on how to write upstart event handlers, or how

####去除第2行至第25行,并把其它的全部打印出来
`sed -n '2,25!p' a`
 1 # inittab is only used by upstart for the default runlevel.
26 id:3:initdefault:

#2、标准正则表达式
```
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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

```
`sed -n '/root/p' b`
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

`sed -n '/^root/p' b`
root:x:0:0:root:/root:/bin/bash

`sed -n '/bash$/p' b`
root:x:0:0:root:/root:/bin/bash

`sed -n '/ro.t/p' b`
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

`sed -n '/roo*/p' b`
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

`sed -n '/[ATP]/p' b`
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

`sed -n '/[A-Z]/p' b`
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

`sed -n '/^[a-c]/p' b`
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

`sed -n '/^[^a-t]/p' b`
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

`sed -n '/\<root/p' b`
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

`sed -n '/^\bno/p' b`
nobody:x:99:99:Nobody:/:/sbin/nologin

#3、扩展正则表达式
####对比1:正则表达式需要使用\转义。显示以root或ftp开头的行
`sed -n '/^root\|^ftp/p' b`
root:x:0:0:root:/root:/bin/bash
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

####对比1:加-r参数后可直接支持扩展正则表达式。
`sed -rn '/^root|^ftp/p' b`
root:x:0:0:root:/root:/bin/bash
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

####对比2:正则表达式需要使用\转义。显示以root或ftp开头的行
`sed -n '/\(^root\|^ftp\)/p' b`
root:x:0:0:root:/root:/bin/bash
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

####对比2:加-r参数后可直接支持扩展正则表达式。
`sed -rn '/(^root|^ftp)/p' b`
root:x:0:0:root:/root:/bin/bash
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

####匹配root和rOOt单词
`sed -rn '/r(oo|OO)t/p' c.txt`

####?匹配0或1次前导字符
`sed -rn '/ro?t/p' c.txt`

####+匹配1-n次前导字符
`sed -rn '/ro+t/p' c.txt`

####匹配只有2次前导字符
`sed -rn '/ro{2}t/p' c.txt`

####匹配2次以上前导字符
`sed -rn '/ro{2,}t/p' c.txt`

####匹配最多2次前导字符
`sed -rn '/ro{,2}t/p' c.txt`

####匹配2-4次前导字符
`sed -rn '/ro{2,4}t/p' c.txt`

####匹配0-n次前导单词
`sed -rn '/(root)*/p' c.txt`

#4、sed编辑(对行的插入、删除、替换操作)
####在有free的行下面插入一行test
`free | sed '/free/a test'`

####在最后一行的下面插入一行test
`free | sed '$a test'`

####将把有free的行替换为123
`free | sed '/free/c 123'`

####删除含有free的行
`free | sed '/free/d'`

####在原文本第2行的上面插入一行test
`free | sed '2i test'`

####在原文本第2行的下面插入一行test
`free | sed '2a test'`

sed命令练习1

转载请注明:林志斌 » sed命令练习2