#!/bin/bash
format(){
usedifs="$IFS"
IFS=$'\n';[ $# -eq 0 ] && { echo "not files need format." ;exit 1; }
for((i=1;i<=$#;i++));do files=(${files[@]} ${!i});done
log1(){
cat ${files[@]}|sed '/^[a-z]\+:/!d;s/[ \t]\+//g'|xargs -n3|\
awk 'BEGIN{FS="[ :]"}
{ x=$1;y=$3;z=$5
a[$2]+=$4
b[$2]=b[$2]?b[$2]","$NF:$NF
}END{
for(i in a){
split(b[i],t,",");
for(j in t)p["\""t[j]"\""]
for(k in p)v=v?v","k:k
print "{\""x"\":\""i"\",\""y"\":\""a[i]"\",\""z"\":["v"]}"
delete t
delete p
v=""}
}'|paste -s -d ','
}
log2(){
cat ${files[@]}|egrep '^([0-9]\.?){1,4}'|awk '{a[$1]+=$2}END{for(i in a){print "{\"ip\":\""i"\",\"times\":\""a[i]"\"}"}}'|paste -s -d ','
}
echo -e "{\n\t\"log1\":[$(log1)],\n\t\"log2\":[$(log2)]\n}"
IFS="$usedifs"
}
format "*.log"
id: 766cbd
times: 6
type: index, observe, forecast3d
id: 7c1429
times: 5459142
type: index, forecast5d, observe
id: 334cfc
times: 144
type: index, forecast3d, alarm
id: 549d81
times: 1775193
type: forecast, index, observe, alarm, calendar
id: 71f520
times: 1638
type: index, forecast3d, air
61.4.184.83 17480
61.4.184.81 1664154
61.4.184.93 5810487
61.4.184.91 5771866
61.4.184.82 17472
61.4.184.90 5757377
61.4.184.92 5798871