linux - Combine results of column one Then sum column 2 to list total for each entry in column one -


i bit of bash newbie, please bear me here.

i have text file dumped software (that have no control over) listing each user number of times accessing resource looks this:

 jim 109 bob 94 john 92 sean 91 mark 85 richard 84 jim  79 bob  70 john 67 sean 62 mark 59 richard 58 jim  57 bob  55 john 49 sean 48 mark 46 . . . 

my goal here output this.

 jim  [total jim] bob  [total bob] john [total john] 

and on.

names change each time run query in software, static search on each name , piping through wc not help.

this sounds job awk :) pipe output of program following awk script:

your_program | awk '{a[$1]+=$2}end{for(name in a)print name " " a[name]}' 

output:

sean 201 bob 219 jim 245 mark 190 richard 142 john 208 

the awk script can explained better in format:

# executed on each line {   # 'a' array. initialized    # empty array awk on it's first usage   # '$1' contains first column - name   # '$2' contains second column - amount   #   #  on every line total score of 'name'    #  incremented  'amount'   a[$1]+=$2 } # executed @ end of input end{   # print every name , score   for(name in a)print name " " a[name] } 

note, output sorted score, can add pipe sort -r -k2. -r -k2 sorts second column in reverse order:

your_program | awk '{a[$1]+=$2}end{for(n in a)print n" "a[n]}' | sort -r -k2 

output:

jim 245 bob 219 john 208 sean 201 mark 190 richard 142 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -