Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find maximum values in file

Status
Not open for further replies.

vidmo

Technical User
Sep 18, 2006
14
GB
Hi, is it possible find the maximum depth in $2 for every different value in $1(a coordinate)?. The depth in the file ranges to a maximum of around -14 and always starts at -1.47. $4 is "actual" depth

486 -1.47 1.83712 -3.35
486 -1.97 1.67761 -3.35
486 -2.47 1.4363 -3.35
486 -2.97 2.89847 -3.35
487 -1.47 1.81872 -3.43
487 -1.97 1.66125 -3.43
488 -1.47 1.41585 -3.36

so just to clarify
###################################################
while $1 == 486; conc=2.89847 (the corresponding value in $3 at the maximum value of $2 (-2.97), and actual=$4 of the same line (-3.35).

awk "other awk program requiring $conc and $actual"

loop back and repeat for the next coordinate in $1 (487)
conc=1.66125
actual=-3.43
continue to the end of file
###################################################
i hope this makes sense and that someone can help
 

Try this:
Code:
awk '{if ( $4 > a[$1] ) a[$1] = $4;}
    END {for(i in a)print i,a[i]}' MyFile.txt
[3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
the output from this comes out as:
780
740
781
741
700
782
742
701
which are unordered numbers from $1, it doesn't appear to print "i"
I'm looking for the output of, for example (sorry i realise i didn't actually state very well the desired output):
486 -2.97 2.89847 -3.35
487 -1.97 1.66125 -3.43

so: when awk reaches the last time when $1 is the same number, print out the the whole line (which is the data from the deepest part of the water)
In the simplest way - awk reads $1, first line is: 486, next line: 486, next line: 486, next line: 487, stops, goes back to the previous line and prints $0, continues: 487, 487, 488, stops, print $0 of previous line etc

i don't fully understand the way arrays work in awk yet to be able to fix this

thanks for your help

 
How about:

Code:
awk '
        $2 < depth[$1] {
                depth[$1] = $2
                conc[$1] = $3
                actual[$1] = $4
        }
        END {
                for(i in depth)
                        print i,depth[i],conc[i],actual[i]
        }
' inputfile

By the way, to be pedantic, you are finding the minimum values in the file, because they are negative.

Annihilannic.
 
ok brilliant, thanks. just out of interest (and an attempt to understand awk a bit better) though- the results come out in a peculiar order:
802 -12.97 0.456745 -14.43
885 -11.97 7.04983 -12.11
844 -12.97 1.49356 -13.805
570 -5.47 3.37087 -5.93
803 -12.97 0.27474 -14.36
886 -11.47 4.14797 -12.07
530 -3.47 3.21545 -3.85
845 -12.97 2.13978 -13.71

why is that?

yes, fair enough- they are minimum values, but maximum depths! no wonder i get so confused!!

thanks
vidmo
 
Unfortunately awk does not necessarily store arrays internally in the order we would expect (it uses hash tables), and for (i in depth) processes the entries in that internal order. You can sort the array using the supplied sort functions and change the for loop to use an integer index, or simply pipe the output through sort -n if you wish.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top