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
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 to share