Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
    
Edited script to output exactly like you wanted. –  sputnick Nov 2 '12 at 20:31
    
Thank you sputnick. It works! –  Steve Nov 3 '12 at 17:43
add comment

Your Answer

 
discard

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.