1.输出占用率超过60%的分区
df -h | awk 'BEGIN{print "Full Partition"}NR>1{gsub("%","",$5);$5+=0;if($5>=60){print $1"\t"$5}}'
2. 停止占用8080端口的進程
lsof -i:8080 | awk 'NR!=1{print $2}' | xargs kill -sigkill
3.传入变量
# 不使用 -v参数 echo | awk 'END{print user}' user="$USER"
# 使用 -v 参数 echo | awk -v user="$USER" '{print user}' echo | awk -v user="$USER" 'END{print user}' awk -v user="$USER" 'BEGIN{print user}'
4.查询慢请求
文本格式
[12/Oct/2018:00:00:36 +0800] 10.0.2.40 GET /actuator/health HTTP/1.0 200 ( 0 ms ) [12/Oct/2018:00:00:39 +0800] 10.0.1.40 GET /actuator/health HTTP/1.0 200 ( 1 ms ) [12/Oct/2018:00:00:40 +0800] 10.0.2.40 GET /actuator/health HTTP/1.0 200 ( 1 ms ) [12/Oct/2018:00:00:43 +0800] 10.0.1.40 GET /actuator/health HTTP/1.0 200 ( 2 ms ) [12/Oct/2018:00:00:43 +0800] 10.0.2.40 GET /actuator/health HTTP/1.0 200 ( 1 ms ) [12/Oct/2018:00:00:45 +0800] 127.0.0.1 HEAD /actuator/health HTTP/1.1 200 ( 2 ms ) [12/Oct/2018:00:00:46 +0800] 10.0.1.40 GET /actuator/health HTTP/1.0 200 ( 2 ms )
查询命令: URL是/config/edit 且 处理时长大于100毫秒
awk '{ if ( $(NF-2) > 100 && $5=="/config/edit" ) print $0}' /opt/ali/config-web/backup/access_log.2018-10-*.log