Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk/Unix group by

Tags:

unix

awk

have this text file:

name, age joe,42 jim,20 bob,15 mike,24 mike,15 mike,54 bob,21 

Trying to get this (count):

joe 1 jim 1 bob 2 mike 3 

Thanks,

like image 315
John Williams Avatar asked Feb 17 '13 00:02

John Williams


People also ask

How do you use NR in awk?

NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file. NF: NF command keeps a count of the number of fields within the current input record.


2 Answers

$ awk -F, 'NR>1{arr[$1]++}END{for (a in arr) print a, arr[a]}' file.txt joe 1 jim 1 mike 3 bob 2 

EXPLANATIONS

  • -F, splits on ,
  • NR>1 treat lines after line 1
  • arr[$1]++ increment array arr (split with ,) with first column as key
  • END{} block is executed at the end of processing the file
  • for (a in arr) iterating over arr with a key
  • print a print key , arr[a] array with a key
like image 100
Gilles Quenot Avatar answered Sep 20 '22 12:09

Gilles Quenot


Strip the header row, drop the age field, group the same names together (sort), count identical runs, output in desired format.

tail -n +2 txt.txt | cut -d',' -f 1 | sort | uniq -c | awk '{ print $2, $1 }' 

output

bob 2 jim 1 joe 1 mike 3 
like image 35
nneonneo Avatar answered Sep 19 '22 12:09

nneonneo