- 顯示/proc/meminfo文件中以大寫或小寫s開頭的行;用兩種方式
[root@localhost mytest]# grep -i ^s /proc/meminfo
[root@localhost mytest]# grep ^[Ss] /proc/meminfo
- 顯示/etc/passwd文件中其默認(rèn)shell為非/sbin/nologin的用戶
[root@localhost mytest]# grep -v /sbin/nologin$ /etc/passwd | cut -d ':' -f1
- 顯示/etc/passwd文件中其默認(rèn)shell為/bin/bash的用戶
[root@localhost mytest]# grep '/bin/bash$' /etc/passwd | cut -d ':' -f1
- 找出/etc/passwd文件中的一位數(shù)或兩位數(shù)
[root@localhost mytest]# cat /etc/passwd | grep -E '\<[1-9]?[0-9]\>'
使用選項(xiàng)-E,使支持?jǐn)U展正則,注意限定單詞
- 顯示/boot/grub/grub.conf中以至少一個(gè)空白字符開頭的行
[root@localhost mytest]# cat file.test | grep '^[[:space:]]+'
紅色部分
centos7沒有改文件,新建file.test文件測(cè)試,注意空白字符也是字符,空行不含任何字符
- 顯示/etc/grub2.cfg文件中以#開頭,后面跟至少一個(gè)空白字符,且后又有至少一個(gè)非空白字符的行
[root@localhost mytest]# cat grub2.cfg.test | grep -E '^#[[:space:]]+[^[:space:]]+'
紅色部分的內(nèi)容
- 打出netstat -tan 命令執(zhí)行結(jié)果中以‘LISTEN’,后或跟空白字符結(jié)尾的行
[root@localhost mytest]# netstat -tan | grep -E 'LISTEN[[:space:]]+$'
[root@localhost mytest]# netstat -tan | grep 'LISTEN[[:space:]]+$'
兩種寫法:
前者是支持?jǐn)U展正則,無需引號(hào)也可以
后者是標(biāo)準(zhǔn)正則,注意需要使用引號(hào),注意+的使用區(qū)別
總結(jié)一點(diǎn):在寫正則時(shí),盡量用引號(hào)將匹配的元字符及字符引起來。
- 添加用戶bash,testbash,basher,nologin(此一個(gè)用戶的shell為/sbin/nologin),而后找出當(dāng)前系統(tǒng)上其用戶名和默認(rèn)shell相同的用戶的信息;
[root@localhost mytest]# useradd bash
[root@localhost mytest]# useradd basher
[root@localhost mytest]# useradd testbash
[root@localhost mytest]# useradd -s /sbin/nologin nologin
[root@localhost mytest]# grep -E '^([^:]+>).*\1$' /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
bash:x:2004:2004::/home/bash:/bin/bash
nologin:x:2007:2007::/home/nologin:/sbin/nologin
[root@localhost mytest]# grep -E '^([[:alnum:]]+>).*\1$' /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
bash:x:2004:2004::/home/bash:/bin/bash
nologin:x:2007:2007::/home/nologin:/sbin/nologin
關(guān)鍵的是匹配詞首,然后運(yùn)用后向引用;第一種匹配更全(用戶名肯定是不行冒號(hào)的),第二種就有點(diǎn)僥幸的意思了
- 顯示當(dāng)前系統(tǒng)上root、fedora或user1用戶的默認(rèn)shell
[root@localhost mytest]# grep -E '^(root|fedora|user1)' /etc/passwd
- 找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號(hào)的行,形如:hello();
[root@localhost mytest]# cat /etc/rc.d/init.d/functions | grep -E -o '[[:alnum:]]+\(\)'
主要是括號(hào)的轉(zhuǎn)義
- 使用echo命令輸出一個(gè)絕對(duì)路徑,使用grep取出其基名;取出其路徑名
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# echo $PWD
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# echo $PWD | grep -E -o '[^/]+$'
network-scripts
[root@localhost network-scripts]# echo $PWD | grep -E -o '[^/]+/?$'
network-scripts
[root@localhost network-scripts]# echo /etc/sysconfig/network-scripts/ | grep -E -o '[^/]+$'
[root@localhost network-scripts]# echo /etc/sysconfig/network-scripts/ | grep -E -o '[^/]+/?$'
network-scripts/
[root@localhost network-scripts]#
相對(duì)來說第二種寫法比較全面,包含第一種的可能性,有/或沒有/;所以要把所有的可能都考慮進(jìn)去,推薦第二種寫法
- 找出ifconfig命令結(jié)果中的1-255之間數(shù)字
ifconfig | grep -E '\<[1-9]\>|\<[1-9][0-9]\>|\<1[0-9][0-9]\>|\<2[0-4][0-9]\>|\<25[0-5]\>'
ifconfig | grep -E '\<[1-9][0-9]?\>|\<1[0-9]{2}\>|\<2[0-4][0-9]\>|\<25[0-5]\>'
ifconfig | grep -E '\<([1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>'
寫法一次比一次簡(jiǎn)便,有些地方進(jìn)行了合并。
- 挑戰(zhàn)題:寫一個(gè)模式,能匹配合理的ip地址
ifconfig | grep -E '\<([1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>(.\<([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>){3}'
思路:三個(gè)個(gè)關(guān)鍵點(diǎn)
1. ip地址最左位的十進(jìn)制數(shù),范圍為0-255,但不能為0,01,02,04等,100-199,200-249,250-255就不說了,即應(yīng)該準(zhǔn)確過濾1-9和10-99;前者為[1-9],后者為[1-9][0-9];合并下即為[1-9][0-9]?
2. 其他三位,范圍為0-255,也遵循第一條的情況,但需要把包含0在內(nèi),所以0-9為[0-9],10-99為[1-9][0-9],合并之后為[1-9]?[0-9]
3. {3}次數(shù)的用法
總結(jié):1中的匹配的是數(shù)值大小為1-99,2中匹配的數(shù)值大小為0-99。兩種寫法順序不能顛倒,是有匹配順序的。
- 挑戰(zhàn)題:寫一個(gè)模式,能匹配出所有的郵件地址
^[^-._].*@.*(com|cn)(.cn)?$
這樣的寫法無法控制連續(xù)標(biāo)點(diǎn)符號(hào)的情況
查找/var目錄下屬主為root,且屬組為mail的所有文件或目錄
查找當(dāng)前系統(tǒng)上沒有屬主或?qū)俳M的文件;進(jìn)一步,查找當(dāng)前系統(tǒng)上沒有屬主或?qū)俳M,且最近3天內(nèi)曾被訪問過的文件或目錄
查找/etc目錄下所有用戶都有寫權(quán)限的文件
查找/etc目錄下大于1M,且類型為普通文件的所有文件

