Reputation: 67301
i have text file like this:
i
am
fine
how
are
you
what
i
how
are
i need an output like below:
i : 2
am : 1
fine : 1
how : 2
are : 2
you : 1
what : 1
there can be many repititions of the words: how could i do this using a shell script or an awk?
Upvotes: 1
Views: 251
Reputation: 53986
In Perl:
perl -le'while (<>){ chomp; $seen{$_}++}; print map { $_ . " : " . $seen{$_} } keys %seen'
Upvotes: -1
Reputation: 342769
@OP, if you want to preserve the order
awk ' { a[$0]++; d[NR]=$0 }
END{
for(i=1;i<=NR;i++){
if( ! (d[i] in p) ){
print a[d[i]],d[i]
p[d[i]]
}
}
} ' file
output
$ ./shell.sh
2 i
1 am
1 fine
2 how
2 are
1 you
1 what
Upvotes: 0
Reputation:
sort | uniq -c
It sorts it and the count is by default before the line. Would that work?
Upvotes: 4
Reputation: 96201
awk '{ count[$1]++ }
END { for (a in count) printf("%s : %d\n", a, count[a]) }' filename
awk
has associative arrays, and all the variables are initialized to 0, so the above works as expected.
Upvotes: 1