Reputation: 7
I have a file
1 ABC 123 345 Apples
1 ABC 345 345 Apples
1 ABC 123 345 Apples_Fuji
1 ABC 123 345 ApplesApplesApples
1 ABC 123 345 Pears
1 ABC 123 345 Banana
...
I wish to get an output file
Apples 2
Apples_Fuji 1
ApplesApplesApples 1
Pears 1
Banana 1
...
I'm not sure whether grepping them one at a time would work (-o would be inaccurate anyway, -c is strangely giving me a value of 1 everytime).
Upvotes: 0
Views: 195
Reputation: 98
Solution with cut,sort,uniq
cat test | cut -f5,5 | sort | uniq -c
Upvotes: 1
Reputation: 67231
In Perl:
perl -lane '$h{$F[4]}++ unless(/^\s*$/);END{print "$_ $h{$_}" for(keys %h)}' your_file
Tested Below:
> cat temp
1 ABC 123 345 Apples
1 ABC 345 345 Apples
1 ABC 123 345 Apples_Fuji
1 ABC 123 345 ApplesApplesApples
1 ABC 123 345 Pears
1 ABC 123 345 Banana
> perl -lane '$h{$F[4]}++ unless(/^\s*$/);END{print "$_ $h{$_}" for(keys %h)}' temp
Pears 1
ApplesApplesApples 1
Banana 1
Apples 2
Apples_Fuji 1
>
Upvotes: 0
Reputation: 676
One solution using awk/sort/uniq could be:
cat file|awk '{print $5}'|sort|uniq -c
Upvotes: 0
Reputation: 274612
Try awk
:
$ awk '{arr[$NF]++}END{for(i in arr) print i,arr[i]}' file
ApplesApplesApples 1
Apples 2
Banana 1
Apples_Fuji 1
Pears 1
Here's another way, using grep
and uniq
:
$ grep -oE '[^ ]+$' file | sort | uniq -c
2 Apples
1 Apples_Fuji
1 ApplesApplesApples
1 Pears
1 Banana
Upvotes: 0