?1.命令作用以及參數(shù)詳解
?grep: 全局搜索正則表達式并打印行(Global search REgular expression and Print out the line)作用:文本搜索工具,根據(jù)用戶指定的“模式”對目標文本逐行進行匹配檢查;打印匹配到的行模式:由正則表達式字符及文本字符所編寫的過濾條件
??格式:
grep [OPTIONS] PATTERN [FILE...]
常見選項:
–color=auto 對匹配到的文本著色顯示
-m # 匹配#次后停止
-v 顯示不被pattern匹配到的行
-i 忽略字符大小寫
-n 顯示匹配的行號
-c 統(tǒng)計匹配的行數(shù)
-o 僅顯示匹配到的字符串
-q 靜默模式,不輸出任何信息
-A # after, 后#行
-B # before, 前#行
-C # context, 前后各#行
-e 實現(xiàn)多個選項間的邏輯or關系,如:grep –e ‘cat ’ -e ‘dog’ file
-w 匹配整個單詞
-E 使用ERE,相當于egrep
-F 不支持正則表達式,相當于fgrep
-f file 根據(jù)模式文件處理
-r 遞歸目錄,但不處理軟鏈接
-R 遞歸目錄,但處理軟鏈接
2.案例解析
范例:
grep root /etc/passwd
grep "USER" /etc/passwd
grep 'USER' /etc/passwd
grep whoami /etc/passwd
范例:取兩個文件的相同行
[root@centos8 ~]#cat /data/f1.txt
a
b
1
c
[root@centos8 ~]#cat /data/f2.txt
b
e
f
c
1
2
[root@centos8 ~]#grep -f /data/f1.txt /data/f2.txt
b
c
1
范例:
df | grep '^/dev/sd' |tr -s ' ' %|cut -d% -f5|sort -n|tail -1
范例:
[root@centos8 ~]#ss -nt | grep "^ESTAB" |tr -s ' ' : |cut -d: -f6|sort |uniq -c|sort -nr|head -n3
3 10.0.0.1
1 172.16.4.100
1 172.16.31.188
范例:
[root@centos8 ~]#grep -v "^#" /etc/profile | grep -v '^$'
[root@centos8 ~]#grep -v "^#\|^$" /etc/profile
[root@centos8 ~]#grep -v "^\(#\|$\)" /etc/profile
[root@centos8 ~]#grep -Ev "^(#|$)" /etc/profile
[root@centos8 ~]#egrep -v "^(#|$)" /etc/profile
[root@centos6 ~]#egrep -v '^(#|$)' /etc/httpd/conf/httpd.conf
范例:
[root@centos8 ~]#grep -o 'r..t' /etc/passwd
root
root
root
root
r/ft
rypt
范例:
[root@centos8 ~]#ifconfig | grep -E '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
inet 172.16.0.123 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::c11e:4792:7e77:12a4 prefixlen 64 scopeid 0x20<link>
inet 127.0.0.1 netmask 255.0.0.0
[root@centos8 ~]#ifconfig | grep -E '([0-9]{1,3}.){3}[0-9]{1,3}'
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
inet 172.16.0.123 netmask 255.255.0.0 broadcast 172.16.255.255
inet6 fe80::c11e:4792:7e77:12a4 prefixlen 64 scopeid 0x20<link>
inet 127.0.0.1 netmask 255.0.0.0
[root@centos8 ~]#ifconfig eth0 | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1
10.0.0.8
[root@centos8 ~]#cat regex.txt
([0-9]{1,3}\.){3}[0-9]{1,3}
[root@centos8 ~]#ifconfig | grep -oEf regex.txt
10.0.0.8
255.255.255.0
10.0.0.255
127.0.0.1
255.0.0.0
范例:
[root@centos8 ~]#grep -E 'root|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
xiaoming:x:1002:1002::/home/xiaoming:/bin/bash
roob:x:1003:1003::/home/roob:/bin/bash
[root@centos8 ~]#grep -e 'root' -e 'bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
xiaoming:x:1002:1002::/home/xiaoming:/b
范例:
[root@centos8 ~]#grep -w root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos8 ~]#grep '\<root\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
范例:
[root@centos8 ~]#grep "^\(.*\)\>.*\<\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:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin
[root@centos8 ~]#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:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin
[root@centos8 ~]#egrep "^(.*)\>.*\<\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:1008:1008::/home/bash:/bin/bash
nologin:x:1011:1011::/home/nologin:/sbin/nologin
范例:面試題,算出所有人的年齡總和
[root@centos8 ~]#cat /data/nianling.txt
xiaoming=20
xiaohong=18
xiaoqiang=22
[root@centos8 ~]#cut -d"=" -f2 /data/nianling.txt|tr '\n' + | grep -Eo ".*[0-9]"|bc
60
[root@centos8 ~]#grep -Eo "[0-9]+" /data/nianling.txt | tr '\n' + | grep -Eo ".*[0-9]"|bc
60
3.結尾(~有彩蛋~)
?為了讓乏味的日常不再那么枯燥,給大家準備了一個花活兒 (渲染愛心),如果你感興趣的話,我把具體的腳本展示在下方。?文章來源:http://www.zghlxwxcb.cn/news/detail-486039.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-486039.html
#!/bin/bash
y=1250
for (( yy = 45; yy > 0; yy-- )); do
x=-1140
for (( xx = -120; xx < 0; xx++ )); do
ff=$(echo `awk -v x=${x} -v y=${y} 'BEGIN{printf "%.0f\n",(((x/1000)*(x/1000)+(y/1000)*(y/1000)-1)*((x/1000)*(x/1000)+(y/1000)*(y/1000)-1)*((x/1000)*(x/1000)+(y/1000)*(y/1000)-1)-(x/1000)*(x/1000)*(y/1000)*(y/1000)*(y/1000))*10000000}'`)
if [[ ff -le 0 ]]; then
printf "\e[1;41m \e[0m"
else
printf " "
fi
x=$((${x}+20))
done
printf "\n"
y=$((${y}-50))
done
到了這里,關于運維(SRE)成長之路-第3天 文本處理三劍客之 grep的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!