我们使用grep可以直接定位到包含内容的行的数据,但是有写时候的信息不是一行就能描写清楚的,需要多行数据,这样就需要我们要得到符合条件的附近的行数,可以使用这个命令来完成。
如:
forlinx@ubuntu:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.205.132 netmask 255.255.255.0 broadcast 192.168.205.255
inet6 fe80::b18:75a8:91b1:9c71 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:6d:c3:2f txqueuelen 1000 (以太网)
RX packets 347841 bytes 467575717 (467.5 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68037 bytes 4738938 (4.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (本地环回)
RX packets 2476 bytes 233930 (233.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2476 bytes 233930 (233.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
我们要得到ems33的全部内容,可以使用命令
forlinx@ubuntu:~$ ifconfig | grep -a7 ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.205.132 netmask 255.255.255.0 broadcast 192.168.205.255
inet6 fe80::b18:75a8:91b1:9c71 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:6d:c3:2f txqueuelen 1000 (以太网)
RX packets 347842 bytes 467575960 (467.5 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68037 bytes 4738938 (4.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
这样我们使用grep -A7 将查找到的ens33的行和后面的7行内容显示出来,a-after
同样显示前面n行可以使用 grep -Bn
显示前后n行,可以使用grep -n 来实现;