shell统计

统计

  1. 列出当天访问次数最多的IP前20个
    命令:cut -d- -f 1 /usr/local/apache2/logs/access_log |uniq -c | sort -rn | head -20
  2. 查看当天有多少个IP访问:
    awk ‘{print $1}’ log_file|sort|uniq|wc -l
  3. 查看某一个页面被访问的次数;
    grep “/index.php” log_file | wc -l
  4. 查看每一个IP访问了多少个页面:
    awk ‘{++S[$1]} END {for (a in S) print a,S[a]}’ log_file
  5. 将每个IP访问的页面数进行从小到大排序:
    awk ‘{++S[$1]} END {for (a in S) print S[a],a}’ log_file | sort -n
  6. 查看某一个IP访问了哪些页面:
    grep ^111.111.111.111 log_file| awk ‘{print $1,$7}’
  7. 去掉搜索引擎统计当天的页面:
    awk ‘{print $12,$1}’ log_file | grep ^\”Mozilla | awk ‘{print $2}’ |sort | uniq | wc -l
  8. 查看2009年6月21日14时这一个小时内有多少IP访问:
    awk ‘{print $4,$1}’ log_file | grep 21/Jun/2009:14 | awk ‘{print $2}’| sort | uniq | wc -l
  9. 统计访问日志里每个ip访问次数
    [root@qr logs]# cat a.sh
    #!/bin/bash#将28/Jan/2015全天的访问日志放到a.txt文本
    cat access.log |sed -rn ‘/28\/Jan\/2015/p’ > a.txt #统计a.txt里面有多少个ip访问
    cat a.txt |awk ‘{print $1}’|sort |uniq > ipnum.txt#通过shell统计每个ip访问次数
    for i in `cat ipnum.txt`
    do
    iptj=`cat access.log |grep $i | grep -v 400 |wc -l`
    echo “ip地址”$i”在2015-01-28日全天(24小时)累计成功请求”$iptj”次,平均每分钟请求次数为:”$(($iptj/1440)) >> result.txt
    done
  10. 把100天前的文件打包并且删除
    find [path] -type f -mtime +100 -exec tar rvf tmp.tar –remove-files {} \;

查找

find . -name ‘.sh’ | xargs grep -in ‘48

  1. 查找所有”.h”文件
    find /PATH -name “*.h”
  2. 查找所有”.h”文件中的含有”helloworld”字符串的文件
    find /PATH -name “.h” -exec grep -in “helloworld” {} \;
    find /PATH -name “
    .h” | xargs grep -in “helloworld”
  3. 查找所有”.h”和”.c”文件中的含有”helloworld”字符串的文件
    find /PATH /( -name “.h” -or -name “.c” /) -exec grep -in “helloworld” {} \;
  4. 查找非备份文件中的含有”helloworld”字符串的文件
    find /PATH /( -not -name “*~” /) -exec grep -in “helloworld” {} \;
    注:/PATH为查找路径,默认为当前路径。带-exec参数时必须以\;结尾,否则会提示“find: 遗漏“-exec”的参数”。

运维

  1. lsof |grep deleted
    注:这个deleted表示该已经删除了的文件,但是文件句柄未释放,这个命令会把所有的未释放文件句柄的进程列出来
  2. swap使用排序

    1
    for i in $( cd /proc;ls |grep "^[0-9]"|awk ' $0 >100') ;do sudo awk '/^Swap:/{a=a+$2}END{print '"$i"',a/1024"M"}' /proc/$i/smaps 2>/dev/null ; done | sort -k2nr | head -10
  3. curl -O下载

  4. 正则
    bizType=([^&]+)
    bizType=(.*?)[&|$]