zoukankan      html  css  js  c++  java
  • linux awk

    grep 文本过滤器
    sed 流编辑器
    awk 报告生成器
    格式化以后显示


    awk [option] 'PATTERN {action}' file1 file2

    awk -F"|" 'BEGIN{OFS=":"} {print $1,$2,$3}' test.txt

    #文本字符串用双引号
    awk -F"|" 'BEGIN{OFS=":"} {print $1,"jksong",$2,$3}' test.txt

    #BEGIN 后面不用跟文件名
    awk 'BEGIN {print "one two three"}'

    #输出空白行
    awk 'BEGIN {print}'
    awk 'BEGIN {print ""}'


    awk 变量
    1、内置变量之记录变量
    FS: file separator 读取的文本的分隔符
    RS: record separator 换行符
    OFS: output file separator
    ORS:output record separator

    2、内置变量之数据变量
    NR the number of input records 处理的多个文件的总共的行数
    FNR 当前文件的第多少行

    NF 字段数

    3、自定义变量
    awk -v"name=jksong" 'BEGIN {print name}'
    awk 'BEGIN {name="jksong"; print name}'


    awk 'BEGIN {name="jksong"; printf "%10s ",name}'

    所有的运算都支持

    PATTERN
    1、正则
    awk -F":" '/^r/ {print $1}' /etc/passwd

    2、表达式
    $1~/xx/($1 匹配)
    $1!~/xx/($1 不匹配)
    $1 == "xx"

    awk -F":" '$1~/^r/{print $0}' /etc/passwd
    awk -F":" '$1=="song"{print $0}' /etc/passwd
    awk -F: 'NR>=1 && NR<=2 {print $1,$3,$NF}' /etc/passwd

    3、range patt1, patt2 从1开始匹配到2结束匹配
    awk -F: '/^bin/,/^adm/{print $1,$3,$NF}' /etc/passwd

    4、BEGIN END 仅在awk命令开始前和开始后运行一次
    awk -F: 'BEGIN {print "user name sheell "} NR>=1 && NR<=2 {print $1,$3,$NF} END {print "end of report"}' /etc/passwd

    5、空模式
    对文件的每一行都要处理

    #if
    awk -F":" '{name = $1=="root" ? "admin" : "user"; print name }' /etc/passwd
    awk -F":" '{if($1=="root") print $0}' /etc/passwd
    awk -F":" '{if($3>500) {sum+=$3}}END {print sum}' /etc/passwd

    #while
    awk -F: '{i=1;while(i<=NF){if(length($i)>5) {print $i} i++}}'
    df -hP|awk 'NR>1{ gsub("%", "", $5); if($5>2) {print $0} }'

    #for
    awk 'BEGIN {for(i=0;i<10;i++){print i}}'

    #提前结束对本行的处理,进入下一行
    next
    awk -F: '{if($3%2 == 0) next; print $3," ", $0}' /etc/passwd


    #数组
    下标从1开始
    下标为任意字符串
    不存在下标会自动初始化,判断下标是否存在 index in array


    #统计每个shell的使用量
    awk -F: '{shell[$NF]++} END {for(a in shell ){print a, " ", shell[a]}}' /etc/passwd

    #统计每种状态的量
    netstat -tan|awk 'NR>1 {shell[$NF]++} END {for(a in shell ){print a, " ", shell[a]}}'

    一些常用工具命令

    Linux Web服务器网站故障分析常用的命令

    系统连接状态篇:
    1.查看TCP连接状态
    netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn

    netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}' 或
    netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key," ",state[key]}'
    netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"t",arr[k]}'

    netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn

    netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c

    2.查找请求数请20个IP(常用于查找攻来源):

    netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20

    netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n20

    3.用tcpdump嗅探80端口的访问看看谁最高

    tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20

    4.查找较多time_wait连接

    netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

    5.找查较多的SYN连接

    netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more

    6.根据端口列进程

    netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1

    网站日志分析篇1(Apache):

    1.获得访问前10位的ip地址

    cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10
    cat access.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}'

    2.访问次数最多的文件或页面,取前20

    cat access.log|awk '{print $11}'|sort|uniq -c|sort -nr|head -20

    3.列出传输最大的几个exe文件(分析下载站的时候常用)

    cat access.log |awk '($7~/.exe/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -20

    4.列出输出大于200000byte(约200kb)的exe文件以及对应文件发生次数

    cat access.log |awk '($10 > 200000 && $7~/.exe/){print $7}'|sort -n|uniq -c|sort -nr|head -100

    5.如果日志最后一列记录的是页面文件传输时间,则有列出到客户端最耗时的页面

    cat access.log |awk '($7~/.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100

    6.列出最最耗时的页面(超过60秒的)的以及对应页面发生次数

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

    7.列出传输时间超过 30 秒的文件

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

    8.统计网站流量(G)

    cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'

    9.统计404的连接

    awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort

    10. 统计http status

    cat access.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'
    cat access.log |awk '{print $9}'|sort|uniq -c|sort -rn

    10.蜘蛛分析,查看是哪些蜘蛛在抓取内容。

    /usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'

    网站日分析2(Squid篇)按域统计流量

    zcat squid_access.log.tar.gz| awk '{print $10,$7}' |awk 'BEGIN{FS="[ /]"}{trfc[$4]+=$1}END{for(domain in trfc){printf "%st%dn",domain,trfc[domain]}}'

    数据库篇
    1.查看数据库执行的sql

    /usr/sbin/tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | egrep -i 'SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL'

    系统Debug分析篇
    1.调试命令
    strace -p pid
    2.跟踪指定进程的PID
    gdb -p pid

     
    #某列的和
    sed -n '1,5p' test.txt|awk '{sum+=$1} END {print sum}'
     
    #平均值
    sed -n '1,5p' test.txt|awk '{sum+=$1} END {print sum/NR}'
     
    #某列最大值
    awk 'BEGIN {max = 0} {if ($1+0 > max+0) max=$1} END {print "Max=", max}' data
     
  • 相关阅读:
    Django(app的概念、ORM介绍及编码错误问题)
    Django(完整的登录示例、render字符串替换和redirect跳转)
    Construct Binary Tree from Preorder and Inorder Traversal
    Single Number II
    Single Number
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Binary Tree Zigzag Level Order Traversal
    Recover Binary Search Tree
    Add Binary
  • 原文地址:https://www.cnblogs.com/siqi/p/6833466.html
Copyright © 2011-2022 走看看