天天看点

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
           

如果是多个字符串出现次数,可使用: