shell grep正則表達式


關於grep一些簡單小案例:

#查看系統內存、緩存、交換分區 -e的作用是匹配多個表達式

[root@shell ~]# cat /proc/meminfo|grep -e Mem -e Cache -e Swap

===================================================

查找/etc目錄下的所有文件中的郵件地址;

[root@shell ~]# grep -R -o -n -E '[a-z0-9]+\@[a-z0-9]+\.[a-z]{2,4}' /etc

其中:-R:遞歸,-n表示匹配的行號,-o只輸出匹配內容

-E:支持擴展正則表達式

===================================================

#查找/etc/目錄下文件包含"HOSTNAME"的次數,-c統計匹配次數,-v取反

[root@shell ~]# grep -R -c 'HOSTNAME' /etc/|grep -v "0$"

===================================================

#查找內核日志中eth的行,顯示顏色及行號:

[root@shell ~]# dmesg | grep -n --color=auto 'eth'

===================================================

#用dmesg列出核心信息,再以grep找出含有eth哪行,

在關鍵字所在行的前兩行與后三行也一起找出出來顯示

[root@shell ~]# dmesg |grep -n -A3 -B2 --color=auto 'eth'

===================================================

#統計系統中能登錄的用戶的個數

[root@shell ~]# cat /etc/passwd |grep -c bash$

===================================================

#統計httpd進程數量

[root@shell ~]# ps -ef|grep -c httpd

===================================================

#顯示games 匹配的“-C”前后4行

[root@shell ~]# grep -C 4 'games' --color /etc/passwd

===================================================

#獲取網卡名稱:

[root@shell ~]# ip a |grep -E '^[0-9]' |awk -F:'{print $2}'

===================================================

#截取ip地址,[^]*表示以非空字符作為結束符,[0-9.]*表示

數字和點的結合。

[root@shell ~]# ifconfig eth0|grep -E -o 'inet addr:[^]*'

|grep -o'[0-9.]*'

===================================================

#截取MAC地址:

[root@shell ~]# ifconfig eth0|grep -i hwaddr |awk '{print$5}'

===================================================

#查看指定進程:

[root@shell ~]# ps -ef|grep mysql

root 1688 1645 0 05:02 pts/0  00:00:00 grep mysql

[root@shell ~]# ps -ef |grep svn

root 1684 1645 0 05:16  pts/0  00:00:00 grep svn


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM