示例:\(ab+\(xy\)*\) 其中 \1 = ab+\(xy\)* , \2 = xy
abbbbbxyxyxyabxy
练习:
1, 显示 / proc/meminfo 文件中大小 s 开头的行
grep -i "^s" /proc/meminfo
2, 显示 / etc/passwd 文件中不以 / bin/bash 结尾的行
grep -v "/bin/bash$" /etc/passwd
3, 显示 / etc/passwd 文件中 UID 号最大的用户的用户名
sort -n -t: -k3 /etc/passwd | tail -1 | cut -d: -f1
4, 如果用户 root 存在, 显示其默认的 shell 程序
- grep "^root\>" /etc/passwd &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7
- id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7
5, 找出 / etc/passwd 中的两位或三位数
- grep "[0-9]\{2,3\}" /etc/passwd
- -w // 匹配固定单词
- \<[0-9]\{
- 2,3\
- }\>
- \b[0-9]\{
- 2,3\
- }\b
6, 显示 / etc/rc.d/rc.sysinit 文件中, 至少以一个空白字符开头的且后面为非空白字符的行
grep "^[[:space:]]\+.*[^[:space:]]$" /etc/rc.d/rc.sysinit
7, 找出 "netstat -tan" 命令的结果中, 以 "LISTEN" 后跟 0,1 或多个空白字符结尾的行
netstat -tan | grep "LISTEN[[:space:]]*$"
8, 添加用户 bash,testbash,basher 以及 nologin(其 shell 为 / sbin/nologin): 而后找出 / etc/passwd 文件中用户名同 shell 名的行
- useradd bash /bin/bash
- useradd testbash
- useradd basher
- useradd -s /sbin/nologin nologin
- grep "\(^[[:alnum:]]\+\>\).*\1$" /etc/passwd
9,IP 地址如何匹配?
ip 地址:
- 0-255.0-255.0-255.0-255
- 0-255
- 2 0-4 0-9 2[0-4][0-9]
- 2 5 0-5 25[0-5]
- 1 0-9 0-9 1[0-9][0-9]
- 0 0-9 0-9 [0-9][0-9]
- 0 0 0-9 [0-9]
- 2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[0-9][0-9]|[0-9]\.2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[0-9][0-9]|[0-9]\.2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[0-9][0-9]|[0-9]\.2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[0-9][0-9]|[0-9]
- \((2[0-4][0-9])|(25[0-5])|(1[0-9][0-9])|([0-9][0-9])|([0-9])\)\.\1\.\1\.\1
来源: http://www.bubuko.com/infodetail-3156032.html