天天看點

shell常用正則指令

統計目前檔案下的檔案個數

ls -lR|grep "^-"|wc -l 
           

統計指定檔案的檔案個數

[[email protected] fy-core-front-tianjin]# ls -lR upfile |grep "^-"|wc -l
74765
           

統計dump檔案中處于不同狀态的線程數量

[[email protected] WCCY]# jstack 31229 >/home/my.log
[[email protected] WCCY]# grep java.lang.Thread.State /home/my.log| awk '{print $2$3$4$5}' | sort | uniq -c 
     61 RUNNABLE
      4 TIMED_WAITING(onobjectmonitor)
    325 TIMED_WAITING(parking)
     16 TIMED_WAITING(sleeping)
      5 WAITING(onobjectmonitor)
     36 WAITING(parking)
[[email protected] WCCY]# 
           

取出現過的IP位址

[[email protected] elasticsearch-7.5.0]# ifconfig
ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.214  netmask 255.255.255.255  broadcast 192.168.8.214
        inet6 fe80::533d:995f:856f:b004  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::54e4:5e44:c23a:3bd  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::90ad:40ba:b609:64d7  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:a2:3c:6a  txqueuelen 1000  (Ethernet)
        RX packets 11020687  bytes 6553459973 (6.1 GiB)
        RX errors 1  dropped 249  overruns 0  frame 0
        TX packets 8489335  bytes 2522169977 (2.3 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 16988889  bytes 15473701741 (14.4 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16988889  bytes 15473701741 (14.4 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
           
[[email protected] jm]# ifconfig ens192 |egrep -o "(([0-9]|?[0-9]|1[0-9][0-9]|2[0-9][0-9])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-9][0-9])"
192.168.8.214
255.255.255.255
192.168.8.214
[[email protected] jm]# 
           
sort :對IP部分進行排序。
uniq -c:列印每一重複行出現的次數。(并去掉重複行)
sort -n -r:按照重複行出現的次序倒序排列。
head -n 1:取排在第一位的ip位址
           

根據端口查詢程序ID

1. netstat -apn|grep 6233 |awk '{print $7}' |cut  -d/ -f1
 2. lsof -i:6233 |awk '{print $2}' | tail -n +2

> awk '{ print $7}':取資料的第7域(第7列)
> cut 
-d :自定義分隔符,預設為制表符。
-f :與-d一起使用,指定顯示哪個區域。
> tail -n +2 跳過第一行
           

目前WEB伺服器中聯接次數最多的ip位址

netstat -ntu |awk '{print $5}' |sort | uniq -c| sort -nr
           

檢視日志中通路次數最多的前10個IP

檢視日志中出現100次以上的IP

檢視最近通路量最高的檔案

檢視日志中通路超過100次的頁面

統計某url,一天的通路次數

前五天的通路次數最多的網頁

cat access_log | awk '{print $7}' | uniq -c | sort -n -r | head -20
           

從日志裡檢視該ip在幹嘛

列出傳輸時間超過 30 秒的檔案

cat access_log | awk '($NF > 30){print $7}' | sort -n | uniq -c | sort -nr | head -20
           

列出最最耗時的頁面(超過60秒的)

cat access_log | awk '($NF > 60 && $7~/\.php/){print $7}' | sort -n | uniq -c | sort -nr | head -100
           

檢視字元在文中出現的次數

grep -o abc filename|wc -l
           

如果是多個字元串出現次數,可使用: