正則表達(dá)式就是用于匹配每行輸入的一種模式,模式是指一串字符序列。擁有強(qiáng)大的字符搜索功能。也非常方便的搜索過(guò)濾出我們想要的內(nèi)容。
Linux系統(tǒng):CentOS Linux release 8.1.1911 (Core)
1、找出 ifconfig “網(wǎng)卡名” 命令結(jié)果中本機(jī)的 IPv4 地址
ifconfig | head -n 2 |tail -1 |tr -s " " |cut -d" " -f3
df |tr -s " " |cut -d" " -f5
3、查出用戶(hù) UID 最大值的用戶(hù)名、UID 及 shell 類(lèi)型
cat /etc/passwd | cut -d: -f1,3,7| sort -nt: -k2 |tail -n 1
4、查出 /tmp 的權(quán)限
stat /tmp | head -n 4 |tail -n 1|cut -c10-13
5、顯示 CentOS8 上所有系統(tǒng)用戶(hù)的用戶(hù)名和 UID
cat /etc/passwd |cut -d: -f1,3 | egrep -v "[0-9]{4,}"
6、顯示三個(gè)用戶(hù) root、linuxmi、mi 的 UID 和默認(rèn) shell(A8代替)
cat /etc/passwd |egrep "^(root|A8)" |cut -d: -f1,3
7、使用 egrep 取出 /etc/rc.d/init.d/functions 中顯示文件中符合條件的字符
echo /etc/rc.d/init.d/functions | egrep "[a-z]$"
8、使用egrep取出上面路徑的目錄名
echo /etc/rc.d/init.d/functions | egrep "/.*/"
9、統(tǒng)計(jì) last 命令中以 root 登錄的每個(gè)主機(jī)IP地址登錄次數(shù)
10、利用擴(kuò)展正則表達(dá)式分別表示0-9、10-99、100-199、200-249、250-255
echo {1..255} |egrep "\<[0-9]\>"
egrep "\<1[0-9]\>"
egrep "\<1[0-9][0-9]\>"
egrep "\<2[0-4][0-9]\>"
egrep "\<25[0-5]\>"
11、顯示 ifconfig 命令結(jié)果中所有 IPv4 地址
ifconfig |egrep "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
簡(jiǎn)單點(diǎn)來(lái)說(shuō),正則表達(dá)式是對(duì)一組正在處理的文本的描述。
掃碼二維碼 獲取免費(fèi)視頻學(xué)習(xí)資料
- 本文固定鏈接: http://www.wangchenghua.com/post/7561/
- 轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載必須在正文中標(biāo)注并保留原文鏈接
- 掃碼: 掃上方二維碼獲取免費(fèi)視頻資料