Reputation: 13
I have text files with 3 columns and I want to count the the number of each string appears in column 2.
The second column of file1:
SAM
MATHEW
ALAN
NEELA
JOHN
The second column of file2:
ALAN
NEELA
KAREEM
MOHA
Desired output:
SAM-1
MATHEW-1
ALAN-2
NEELA-2
JOHN-1
KAREEM-1
MOHA-1
Upvotes: 0
Views: 668
Reputation: 47249
This would accomplish it, although the output is unordered:
awk '{ h[$2]++ } END { for(k in h) print k " - " h[k] }' file1 file2
A tally is kept in the h
associative array, and when all files have been processed the results are printed in the END
block.
Upvotes: 1
Reputation: 67319
awk '{a[$2]++;next}END{for(i in a)print i"-"a[i]}' file1 file2
tested:
> cat file1
10993 item 0
11002 item 6
693661 item 7
> cat file2
10993 item 0
11002 item1 6
693661 item2 7
> awk '{a[$2]++;next}END{for(i in a)print i"-"a[i]}' file1 file2
item1-1
item2-1
item-4
>
Upvotes: 0
Reputation: 85913
$ awk '{a[$2]++}END{for(k in a)print k"-"a[k]}' file1 file2
MOHA-1
NEELA-2
JOHN-1
KAREEM-1
ALAN-2
MATHEW-1
SAM-1
Upvotes: 2