Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 3 down vote accepted

Try doing this :

awk -F: '
        for (i=3; i<NF+1; i++) {
            if ($i > max) {
            else if ($i < min || min == 0) {
        print "max = $" max " and min = $" min
' file.txt
share|improve this answer
Edited script to output exactly like you wanted. – Gilles Quenot Nov 2 '12 at 20:31
Thank you sputnick. It works! – Steve Nov 3 '12 at 17:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.