grep
-i:忽略大小写
-n:表示行数
找出含有root的行
# grep root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
找出以root为首的行,需要添加特殊字符"^"
# grep ^root /etc/passwd root:x:0:0:root:/root:/bin/bash
找出以"/sbin/nologin"结尾的行,需要添加特殊字符"$"
# grep /sbin/nologin$ /etc/passwd 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
找出没有出现root的行
# grep -v root /etc/passwd vboxadd:x:992:1::/var/run/vboxadd:/bin/false dhcpd:x:177:177:DHCP server:/:/sbin/nologin
找出既没有root,也没有nologin的行
# grep -v root /etc/passwd|grep -v 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 yb:x:1000:1000:yb:/home/yb:/bin/bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false # grep -E -v root|nologin /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt yb:x:1000:1000:yb:/home/yb:/bin/bash vboxadd:x:992:1::/var/run/vboxadd:/bin/false
grep和rpm结合使用示例
找出文件network-scripts属于哪个安装包 # rpm -qf /etc/sysconfig/network-scripts/ initscripts-9.49.17-1.el7.x86_64 找出initscripts-9.49.17-1.el7.x86_64包的帮助文档在哪里 [root@server initscripts-9.49.17]# rpm -qd initscripts-9.49.17-1.el7.x86_64 /usr/share/doc/initscripts-9.49.17/COPYING /usr/share/doc/initscripts-9.49.17/changes.ipv6 /usr/share/doc/initscripts-9.49.17/ipv6-6to4.howto /usr/share/doc/initscripts-9.49.17/ipv6-tunnel.howto /usr/share/doc/initscripts-9.49.17/static-routes-ipv6 /usr/share/doc/initscripts-9.49.17/sysconfig.txt /usr/share/doc/initscripts-9.49.17/sysvinitfiles /usr/share/man/man1/consoletype.1.gz /usr/share/man/man1/genhostid.1.gz /usr/share/man/man1/ipcalc.1.gz /usr/share/man/man1/netreport.1.gz /usr/share/man/man1/usleep.1.gz /usr/share/man/man8/ifdown.8.gz /usr/share/man/man8/ifup.8.gz /usr/share/man/man8/ppp-watch.8.gz /usr/share/man/man8/service.8.gz /usr/share/man/man8/sushell.8.gz /usr/share/man/man8/sys-unconfig.8.gz /usr/share/man/man8/usernetctl.8.gz 进入帮助文档所在目录,查看配置ip文档 # cd /usr/share/doc/initscripts-9.49.17 # grep -ri 'IPADDR' * --这里的*表示所有文件,r表示递归,i表示忽略大小写 ipv6-6to4.howto:IPADDR=xx.yy.zz.ww [Globally unique IPv4 address] ipv6-6to4.howto:address (IPADDR= above), here '193.94.160.1'. sysconfig.txt: bother to define; you can figure out what "IPADDR" is, I sysconfig.txt: IPADDRn= sysconfig.txt: LOCAL_IP= will be converted to IPADDR by netconf sysconfig.txt: MY_INNER_IPADDR=local IP address of the tunnel interface sysconfig.txt: PEER_OUTER_IPADDR=IP address of the remote tunnel endpoint sysconfig.txt: MY_OUTER_IPADDR=IP address of the local tunnel endpoint sysconfig.txt: PEER_INNER_IPADDR=IP address of the remote end of the tunnel interface sysconfig.txt: If this is specified, a route to PEER_INNER_IPADDR through the tunnel # more sysconfig.txt