文本文件


[root@chbo sed.test]# cat test

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

[root@chboa sed.test]# nl test|sed -n '/root/p' test

root:x:0:0:root:/root:/bin/bash

操作实例


1.打印5至7行

[root@chbo sed.test]# nl test|sed -n '5,7p'

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

2.打印含有root的行

[root@chbo sed.test]# nl test|sed -n '/root/p'

1 root:x:0:0:root:/root:/bin/bash

3.打印非nologin的行

[root@chboa sed.test]# nl test|sed -n '/nologin$/!p'

1 root:x:0:0:root:/root:/bin/bash

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

4.在非nologin行前插入新行,内容为:I am chbo_yang.

[root@chbo sed.test]# nl test|sed '/nologin$/!iI am chbo_yang.'

I am chbo_yang.

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

I am chbo_yang.

6 sync:x:5:0:sync:/sbin:/bin/sync

I am chbo_yang.

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

I am chbo_yang.

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

5.将出现的第一个root替换为chbo

[root@chbo sed.test]# nl test|sed -n 's/root/chbo/p'

1 chbo:x:0:0:root:/root:/bin/bash

6.将全文中的root替换成chbo

[root@chbo sed.test]# nl test|sed -n 's/root/chbo/gp'

1 chbo:x:0:0:chbo:/chbo:/bin/bash

7.将2至9行替换为I am chbo.

[root@chboa sed.test]# cat -n test|sed '2,9c I am chbo.'

1 root:x:0:0:root:/root:/bin/bash

I am chbo.

10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

打印本机IP

这个只是随便玩的,方法有很多,组合是任意的。

目标:在屏幕上显示一个ip地址即可。

注意:这里主要使用sed命令,grep,cut,awk等命令暂时不做考虑。

目标处理文件:


[root@chboa ~]# awk '{print NR,$0}' /etc/sysconfig/network-scripts/ifcfg-eth0

1 DEVICE=eth0

2 ONBOOT=yes

3 NM_CONTROLLED=yes

4 BOOTPROTO=none

5 IPADDR=192.168.1.199

6 NETMASK=255.255.255.0

7 GATEWAY=192.168.1.1

8 DNS1=8.8.8.8

9 IPV6INIT=no

10 USERCTL=no

11 TYPE=Ethernet

12 HWADDR=00:0c:29:a3:74:d4

注解:cat -n ;nl ;less -N 都可以给文本加行号

[root@chboa ~]# ifconfig eth0

eth0 Link encap:Ethernet HWaddr 00:0C:29:A3:74:D4

inet addr:192.168.1.199 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fea3:74d4/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:3463 errors:0 dropped:0 overruns:0 frame:0

TX packets:1262 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:331487 (323.7 KiB) TX bytes:194756 (190.1 KiB)

命令处理:

[root@chboa ~]# sed -n 's/IPADDR=//p' /etc/sysconfig/network-scripts/ifcfg-eth0

192.168.1.199

[root@chboa ~]# ifconfig eth0|sed -n 's#^.*addr:\(.*\) Bcast.*$#\1#gp'

192.168.1.199

拓展


[root@chboa ~]# grep -i ipaddr /etc/sysconfig/network-scripts/ifcfg-eth0|awk -F= '{print $2}'

192.168.1.199

[root@chboa ~]# grep -i ipaddr /etc/sysconfig/network-scripts/ifcfg-eth0|cut -d"=" -f2

192.168.1.199

[root@chboa ~]# ifconfig eth0|awk -F"[ :]" 'NR==2{print $13}'

192.168.1.199

[root@chboa ~]# ifconfig eth0|awk -F"[ :]+" 'NR==2{print $4}'

192.168.1.199

[root@chboa ~]# awk -F= 'NR==5{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth0

192.168.1.199