begineerlevel
begineerlevel

Reputation: 25

how to count occurences in specific column and sum it up

I've just started using awk,

so this is the input

No | Band 
1  | 2G
2  | 3G
3  | 4G
4  | 2G
5  | 2G

and the output is

Band | Sum
2G   | 3
3G   | 1
4G   | 1

Thank you very much

Upvotes: 1

Views: 51

Answers (2)

shellter
shellter

Reputation: 37288

Try this

echo "No | Band 
1  | 2G
2  | 3G
3  | 4G
4  | 2G
5  | 2G "\
| awk -F'|' 'NR>1{
      band[$2]++
  }
  END{
    print "Band | Sum"
      for (x in band){
        print x" | " band[x]
      }
  }'

output

Band | Sum
 3G | 1
 2G | 3
 4G | 1

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249293

tail -n +2 input.txt | cut -d'|' -f2 | sort | uniq -c
      3  2G
      1  3G
      1  4G

Upvotes: 2

Related Questions