bash - shell script: count lines and sum numbers from multiple files -


i want count number of lines in .csv file, , sum last numeric column. need make crontab, so, has script.

this code counts number of lines:

egrep -i string file_name_201611* | \ egrep -i "cdr,20161115" | \ awk -f"," '{print $4}' | sort | uniq | wc -l 

this code sums last column:

egrep -i string file_name_201611* | \ egrep -i ".cdr,20161115"| \ awk -f"," '{print $8}' | paste -s -d"+" | bc 

lines looks like:

comgprs,cgsco05,comgprs_cgsco05_400594.dat,processed_cdr_20161117100941_00627727.cdr,20161117095940,20161117,18,46521 

the expected output:

cgsco05,sum_#_lines, sum_$8 cgsco05, 225, 1500 

this should work...

#!/usr/bin/awk -f  begin{     k=0;     fs="," } {     if ($2 in counter){         counter[$2] = counter[$2] + 1;         sum_8[$2] = sum_8[$2] + $8;     }else{         k = k + 1;         counter[$2] = 1;         sum_8[$2] =  $8;         name[k] = $2;     }  } end{     (i=1; i<=k; i++)         printf "%s, %i, %i\n", name[k], counter[name[k]], sum_8[name[k]]; } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -