1

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

1

2 Answers 2

2

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
0
2
tail -n +2 input.txt | cut -d'|' -f2 | sort | uniq -c
      3  2G
      1  3G
      1  4G
2
  • 1
    you need to add some more pipes and commands to switch the column order and get a header row and put a "|" between fields. Maybe a sed and an echo?
    – Ed Morton
    Nov 14, 2014 at 3:48
  • 1
    @EdMorton: that's true, but SO isn't a free programming service so I left the trivial parts as an exercise. :) Nov 14, 2014 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.