- --color=auto 添加色彩 alias grep='grep --color=auto'
- -n 打印包含关键字的行号 `grep -n root /etc/passwd`
- -i 忽略大小写匹配
- grep '^root' /etc/passwd 以 root 开头
- grep 'bash$' /etc/passwd 以 bash 结尾
- -v 取反，grep -v '^root' /etc/passwd 不以 root 开头的文件  
- -A 搜索值的后几行连同展示 grep -niA "^root" /etc/passwd ![199d047fd3786c3065bb4442a7268a82.png](../../../_resources/199d047fd3786c3065bb4442a7268a82.png)
- -B 搜索行的前几行连同展示 ![99e455bd412ce7823ce72bda88e93c7d.png](../../../_resources/99e455bd412ce7823ce72bda88e93c7d.png)
- -w 按照单词搜索，包含单词的行 grep -w "hello" /etc/passwd ![108a61ca70a4bcca4a791ab64600503a.png](../../../_resources/108a61ca70a4bcca4a791ab64600503a.png)
- -o 只打印关键字本身
- 过滤空行 grep -v '^$' 1.txt
- -E 使用扩展正则 `grep -E 'A[0-9]{3}' 1.txt`
```
me@me-EQ59:~/shell_demo$ ifconfig enp2s0 # 取出 ip 子网掩码 和 网关地址
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.110.2  netmask 255.255.255.0  broadcast 192.168.110.255
        inet6 fe80::eedd:38e2:edd:f678  prefixlen 64  scopeid 0x20<link>
        ether 7c:83:34:b4:b0:a3  txqueuelen 1000  (以太网)
        RX packets 178803597  bytes 90913766807 (90.9 GB)
        RX errors 0  dropped 9947  overruns 0  frame 0
        TX packets 177733548  bytes 81380631943 (81.3 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

me@me-EQ59:~/shell_demo$ ifconfig enp2s0 | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"
192.168.110.2
255.255.255.0
192.168.110.255
me@me-EQ59:~/shell_demo$ ifconfig enp2s0 | grep -oP "(\d{1,3}\.){3}\d{1,3}"
192.168.110.2
255.255.255.0
192.168.110.255
me@me-EQ59:~/shell_demo$ 
```