0

bash代码片段

 2 years ago
source link: http://pein0119.github.io/2015/11/27/bash%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

bash代码片段

注:本文会持续更新

  • 以冒号为分隔符,将字符串切分为数组
IFS=: read -r -a config <<< "$d"
  • 获取一分钟前的时刻
TIME=$(date -d "1 minute ago" "+%Y-%m-%d %H:%M")
echo $TIME
  • 日志分析脚本

分析接口运行时间的分布

awk '{split($6, request, "?"); api_stat[request[1], "count"] += 1; time=$(NF-2); gsub(/[\[|\]]/, "", time); if (time <= 0.5) {api_stat[request[1], "count1"] += 1;} if (time > 0.5 && time <= 1.0) {api_stat[request[1], "count2"] += 1;} if (time > 1.0 && time <= 1.5) {api_stat[request[1], "count3"] += 1;} if (time > 1.5 && time <= 2.0) {api_stat[request[1], "count4"] += 1;} if (time > 2.0 && time <= 2.5) {api_stat[request[1], "count5"] += 1;} if (time > 2.5 && time <= 3.0) {api_stat[request[1], "count6"] += 1;} if (time > 3.0) {api_stat[request[1], "count7"] += 1;}} END {for (api in api_stat) { split(api, key, SUBSEP); print key[1], key[2], api_stat[api];} }'
awk -F '[][]' '{api_stat[$6, "count"] += 1; if ($8 <= 50.0) {api_stat[$6, "count1"] += 1;} if ($8 > 50.0 && $8 <= 100.0) {api_stat[$6, "count2"] += 1;} if ($8 > 100.0 && $8 <= 150.0) {api_stat[$6, "count3"] += 1;} if ($8 > 150.0 && $8 <= 200.0) {api_stat[$6, "count4"] += 1;} if ($8 > 200.0 && $8 <= 250.0) {api_stat[$6, "count5"] += 1;} if ($8 > 250.0 && $8 <= 300.0) {api_stat[$6, "count6"] += 1;} if ($8 > 300.0 && $8 <= 350.0) {api_stat[$6, "count7"] += 1;} if ($8 > 350.0 && $8 <= 400.0) {api_stat[$6, "count8"] += 1;} if ($8 > 400.0 && $8 <= 450.0) {api_stat[$6, "count9"] += 1;} if ($8 > 450.0 && $8 <= 500.0) {api_stat[$6, "count10"] += 1;} if ($8 > 500.0) {api_stat[$6, "count11"] += 1;} } END {for(api in api_stat) { split(api, key, SUBSEP); print key[1], key[2], api_stat[api];}}' doota_curl_time_003*
  • 命令行格式化json
python -mjson.tool

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK