awk - How to sum numbers by a specific word separated by semi colon in bash -


this question has answer here:

to more specific on path instance have file below;

file.txt

apple;10; tomato;5; apple;5; banana;10; banana;5; tomato;10; banana;10; apple;5; 

i want sum numbers item using bash result ;

file.output

apple;20; banana;25; tomato;15; 

another word need simplify file. how can achieve kind of output separate file in bash ?

thanks in advance.

try following awk, if need output in same sequence input_file following may in same.

awk -f";" '!a[$1]{b[++i]=$1} {a[$1]+=$2;} end{for(j=1;j<=i;j++){print b[j] fs a[b[j]] fs}}'  input_file 

output follows.

apple;20; tomato;15; banana;25; 

if don't bother sequence per input_file try following:

awk -f";" '{a[$1]+=$2;} end{for(i in a){print fs a[i] fs}}'  input_file 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -