Using awk/nawk, how to find out max and min contributions and printout those values?

1

Using awk/nawk, how can I find max and min contributions and print those values?

Input file:

Name: Phone:1st:2nd:3rd contribution
Mike Harrington:(xxx) xxx-xxxx:250:100:175
Christian Dobbins:(xxx) xxx-xxxx:155:350:201
Susan Dalsass:(xxx) xxx-xxxx:280:60:50
Archie McNichol:(xxx) xxx-xxxx:250:100:175

Expected results would be max = $350 and min = $50

Steve

Posted 2012-11-02T17:04:28.787

Reputation: 287

Answers

3

Try doing this :

awk -F: '
    NR>1{
        for (i=3; i<NF+1; i++) {
            if ($i > max) {
                max=$i
            }
            else if ($i < min || min == 0) {
                min=$i
            }
        }
    }
    END{
        print "max = $" max " and min = $" min
    }
' file.txt

Gilles Quenot

Posted 2012-11-02T17:04:28.787

Reputation: 3 111

Edited script to output exactly like you wanted. – Gilles Quenot – 2012-11-02T20:31:08.647