Saturday, 15 June 2013

bash - Unix AWK field seperator finding sum of one field group by other -



bash - Unix AWK field seperator finding sum of one field group by other -

i using below awk command returning me unique value of parameter $11 , occurrence of in file output separated commas. along looking sum of parameter $14(last value) in output. please help me on it.

sample string in file

exstat|bnk|2014|11|05|15|29|46|23169|e582754245|qabd|s|000|351

$14 lastly value 351

bash-3.2$ grep 'exstat|' abc.log|grep '|s|' | awk -f"|" '{ a[$11]++ } end { (b in a) { print b"," a[b] ; } }' qder,3 qcol,1 qasm,36 qbend,23 qast,3 qglbe,30 qcd,30 tbeno,1 qabd,9 qabe,5 qdcd,5 tesub,1 qfde,12 qcpa,3 qadt,80 qlsmr,6 bash-3.2$ grep 'exstat|' abc.log exstat|bnk|2014|11|05|15|29|03|23146|e582754222|qglbe|s|000|424 exstat|bnk|2014|11|05|15|29|05|23147|e582754223|qcd|s|000|373 exstat|bnk|2014|11|05|15|29|12|23148|e582754224|qasm|s|000|1592 exstat|bnk|2014|11|05|15|29|13|23149|e582754225|qadt|s|000|660 exstat|bnk|2014|11|05|15|29|14|23150|e582754226|qadt|s|000|261 exstat|bnk|2014|11|05|15|29|14|23151|e582754227|qadt|s|000|250 exstat|bnk|2014|11|05|15|29|15|23152|e582754228|qadt|s|000|245 exstat|bnk|2014|11|05|15|29|15|23153|e582754229|qadt|s|000|258 exstat|bnk|2014|11|05|15|29|17|23154|e582754230|qadt|s|000|261 exstat|bnk|2014|11|05|15|29|18|23155|e582754231|qadt|s|000|263 exstat|bnk|2014|11|05|15|29|18|23156|e582754232|qadt|s|000|250 exstat|bnk|2014|11|05|15|29|19|23157|e582754233|qadt|s|000|270 exstat|bnk|2014|11|05|15|29|19|23158|e582754234|qadt|s|000|264 exstat|bnk|2014|11|05|15|29|20|23159|e582754235|qadt|s|000|245 exstat|bnk|2014|11|05|15|29|20|23160|e582754236|qadt|s|000|241 exstat|bnk|2014|11|05|15|29|21|23161|e582754237|qadt|s|000|237 exstat|bnk|2014|11|05|15|29|21|23162|e582754238|qadt|s|000|229 exstat|bnk|2014|11|05|15|29|22|23163|e582754239|qadt|s|000|234 exstat|bnk|2014|11|05|15|29|22|23164|e582754240|qadt|s|000|237 exstat|bnk|2014|11|05|15|29|23|23165|e582754241|qadt|s|000|254 exstat|bnk|2014|11|05|15|29|23|23166|e582754242|qadt|s|000|402 exstat|bnk|2014|11|05|15|29|24|23167|e582754243|qadt|s|000|223 exstat|bnk|2014|11|05|15|29|24|23168|e582754244|qadt|s|000|226

just add together associative array:

awk -f"|" '{a[$11]++;c[$11]+=$14}end{for(b in a){print b"," a[b]","c[b]}}'

tested below:

> cat temp exstat|bnk|2014|11|05|15|29|03|23146|e582754222|qglbe|s|000|424 exstat|bnk|2014|11|05|15|29|05|23147|e582754223|qcd|s|000|373 exstat|bnk|2014|11|05|15|29|12|23148|e582754224|qasm|s|000|1592 exstat|bnk|2014|11|05|15|29|13|23149|e582754225|qadt|s|000|660 exstat|bnk|2014|11|05|15|29|14|23150|e582754226|qadt|s|000|261 exstat|bnk|2014|11|05|15|29|14|23151|e582754227|qadt|s|000|250 exstat|bnk|2014|11|05|15|29|15|23152|e582754228|qadt|s|000|245 exstat|bnk|2014|11|05|15|29|15|23153|e582754229|qadt|s|000|258 exstat|bnk|2014|11|05|15|29|17|23154|e582754230|qadt|s|000|261 exstat|bnk|2014|11|05|15|29|18|23155|e582754231|qadt|s|000|263 exstat|bnk|2014|11|05|15|29|18|23156|e582754232|qadt|s|000|250 exstat|bnk|2014|11|05|15|29|19|23157|e582754233|qadt|s|000|270 exstat|bnk|2014|11|05|15|29|19|23158|e582754234|qadt|s|000|264 exstat|bnk|2014|11|05|15|29|20|23159|e582754235|qadt|s|000|245 exstat|bnk|2014|11|05|15|29|20|23160|e582754236|qadt|s|000|241 exstat|bnk|2014|11|05|15|29|21|23161|e582754237|qadt|s|000|237 exstat|bnk|2014|11|05|15|29|21|23162|e582754238|qadt|s|000|229 exstat|bnk|2014|11|05|15|29|22|23163|e582754239|qadt|s|000|234 exstat|bnk|2014|11|05|15|29|22|23164|e582754240|qadt|s|000|237 exstat|bnk|2014|11|05|15|29|23|23165|e582754241|qadt|s|000|254 exstat|bnk|2014|11|05|15|29|23|23166|e582754242|qadt|s|000|402 exstat|bnk|2014|11|05|15|29|24|23167|e582754243|qadt|s|000|223 exstat|bnk|2014|11|05|15|29|24|23168|e582754244|qadt|s|000|226 > awk -f"|" '{a[$11]++;c[$11]+=$14}end{for(b in a){print b"," a[b]","c[b]}}' temp qglbe,1,424 qadt,20,5510 qasm,1,1592 qcd,1,373 >

also check test here

bash shell unix awk

No comments:

Post a Comment