grep命令基本用法
1、grep正則表達式(對文本行進行搜索過濾)
格式:grep [option] PATTERN [FILE...] option: -i:忽略大小寫 -v:取反,顯示未被匹配到的pattern -n:顯示匹配的行號 -c:統計匹配的行數 -o: 只顯示匹配到的pattern -q: 靜默,不予顯示 -A#:after,匹配到的行再向后顯示#行 -B#:before,匹配到的行再向前顯示#行 -C#:context,向前向后各顯示#行 -E :等同于egrep(擴展的正則表達式)
2、正則表達式元字符
字符匹配
.:任意單個字符 []:匹配[]以內的任意單個字符 [^]:匹配[]以外的任意單個字符 [:digit:]:十進制數字 [:upper:]:大寫字母 [:lower:]:小寫字母 [:space:]:空格 [:alnum:]:字母及數字 [:alpha:]:大小寫字母 eg:
[root@Centos6 ~]#grep r..t /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@Centos6 ~]#grep r[a-z]t /etc/passwd operator:x:11:0:operator:/root:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [root@Centos6 ~]#grep r[^0-9][a-z]t /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
次數匹配
*:前面字符任意次 \?:前面字符0或1次 .*:任意字符任意次 \+:至少一次 \{m,n\}:大于m小于n次
*:前面字符任意次 [root@Centos6 ~]#grep ro*t /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin \?:前面字符0或1次 [root@Centos6 ~]#grep "ro\?" /etc/passwd root:x:0:0:root:/root:/bin/bash adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin .*:任意字符任意次 [root@Centos6 ~]#grep ro.*t /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin \+:至少一次 [root@Centos6 ~]#grep "ro\+" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin \{m,n\}:大于m小于n次 [root@Centos6 ~]#grep "ro\{1,5\}" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin chrony:x:992:989::/var/lib/chrony:/sbin/nologin setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin rooooot:x:1001:1001::/home/rooooot:/bin/bash rooot:x:1002:1002::/home/rooot:/bin/bash
位置錨定
對于行來說 ^:行首錨定 $:行尾錨定 ^$:空行 ^ $:空格 對于詞來說 \<,\b:詞首錨定 \>,\b:詞尾錨定
[root@Centos6 ~]#grep "^root" /etc/passwd root:x:0:0:root:/root:/bin/bash [root@Centos6 ~]#grep "bash$" /etc/passwd root:x:0:0:root:/root:/bin/bash mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash ymd:x:500:500:ymd:/home/ymd:/bin/bash [root@Centos6 ~]#grep "\<bin\>" /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash ymd:x:500:500:ymd:/home/ymd:/bin/bash
分組
\(\):將一個或多個字符作為一個整體,進行處理。 后向引用:引用前面分組括號中所匹配到的內容 \1,\2 ··· eg:
grep "\(string1\+\(string2\)*\)" \1:string1\+\(string2\)* \2:string2
擴展的正則表達式(egrep)
egrep等同于grep -E 其選項及參數大致與grep相同
字符匹配
.:任意單個字符 []:匹配[]以內的任意單個字符 [^]:匹配[]以外的任意單個字符
次數匹配
*:前面字符任意次 ?:前面字符0或1次 .*:任意字符任意次 +:至少一次 {m,n}:大于m小于n次
3、利用所學知識如何在生產環境中實現對磁盤最大使用空間的監控
在實際的生產環境中我們需要實時的查看及監控磁盤空間情況防止因磁盤空間不足導致服務器崩潰 因此我們需要查看磁盤利用率是否超過限制值。
此為利用現在所學的命令完成查看磁盤已使用的空間情況 [root@Centos6 ~]#df -h |grep "^/dev/sda"|tr -s " " | cut -d " " -f5|sort -nr|head -n1 10%
未完待續。。。
原創文章,作者:M25_ymd,如若轉載,請注明出處:http://www.www58058.com/81774