How do I write a shell script that filters lines and counts them?

I have a text file:

Apple
Orange
Orange
Banana
Banana
Orange
Banana
Orange
Apple
Orange

      

I want to generate the following output after running a shell bash script:

Apple: 2
Orange: 5
Banana: 3

      

This is pretty standard stuff if I'm using a full blown language like Java / C ++ etc., but what's the fastest way to do this with a shell script / command line?

+2


source to share


4 answers


sort $FILE | uniq -c

      

will provide you



2 Apple
3 Banana
5 Orange

      

+13


source


sort file name | uniq -c | awk '{print $ 2 ":" $ 1}'



+4


source


This solution only uses one tool: awk

$ awk '{count[$0]++} END {for (c in count) {print c ": " count[c]}} ' count.txt
Orange: 5
Banana: 3
Apple: 2

      

+4


source


uniq -c $FILE | perl -pe 's|[ ]*([0-9]+)[ ]*(.*)|\2: \1|'
      

This will format it as indicated. You can add '| sort 'at the end of the sort too.

EDIT: As stated in the comment, I am wrong about uniq, so here's the fix.

sort $FILE | uniq -c | perl -pe 's|[ ]*([0-9]+)[ ]*(.*)|\2: \1|'
      

Sorry for the problem.

+2


source







All Articles