views:

86

answers:

4

This was an interview question, nevertheless still a programming question.

I have a unix file with two columns name and score. I need to display count of all the scores.

like

jhon 100
dan 200
rob 100
mike 100

the output should be

100 3
200 1

You only need to use built in unix utility to solve it, so i am assuming using shell scripts . or reg ex. or unix commands

I understand looping would be one way to do. store all the values u have already seen and then grep every record for unseen values. any other efficient way of doing it

A: 

Not very complicated in perl:

#!/usr/bin/perl -w
use strict;
use warnings;

my %count = ();

while (<>) {
        chomp;
        my ($name, $score) = split(/ /);
        $count{$score}++;
}

foreach my $key (sort keys %count) {
        print "$key ", $count{$key}, "\n";
}
hlovdal
+1  A: 

Try this:

cut -d ' ' -f 2 < /tmp/foo | sort -n | uniq -c \
    | (while read n v ; do printf "%s %s\n" "$v" "$n" ; done)

The initial cut could be replaced with another while read loop, which would be more resilient to input file format variations (extra whitespace). If some of the names consist in several words, simple field extraction will not work as easily, but sed can do it.

Otherwise, use your favorite programming language. Perl would probably shine. It is not difficult either in Java or even in C or Forth.

Thomas Pornin
A: 

You could go with awk:

awk '/.*/ { a[$2] = a[$2] + 1; } END { for (x in a) { print x, " ", a[x] } }' record_file.txt
nichromium
A: 

Alternatively with shell commands:

for i in `awk '{print $2}' inputfile | sort -u`
do
    echo -n "$i "
    grep $i inputfile | wc -l
done

The first awk command will give a list of all the different scores (e.g. 100 and 200) which then the for loop iterates over, counting up each separately. Not very super efficient, but simple. If the file is not to big is should not be a too big problem.

hlovdal