views:

98

answers:

4

Hi , 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?

+1  A: 
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.

Alok
+4  A: 
sort | uniq -c

It sorts it and the count is by default before the line. Would that work?

Moron
yes this works:)
Vijay Sarathi
This one deserves to be the accepted answer IMHO.
Ether
A: 

In Perl:

perl -le'while (<>){ chomp; $seen{$_}++}; print map { $_ . " : " . $seen{$_} } keys %seen'
Ether
Oops I forgot the increment! (Several months later...) :)
Ether
A: 

@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
ghostdog74