find global min/max among local extreme using awk or shell script -
i can find local min. or local max. of 2 column data (x, y) using following awk codes.
awk 'prev!=""&&prev<=prev2&&prev<=$2{print line}{prev2=prev;prev=$2;line=$0}' file # local min. awk 'prev!=""&&prev>=prev2&&prev>=$2{print line}{prev2=prev;prev=$2;line=$0}' file # local max.
qs:
could explain above codes shortly? used , saw works, don't know how works...
how find global min/max after local min/max found above code?
local minimum explained
let's consider code local minimum. uses 3 variables:
prev2
value of second column 2 lines agoprev
value of second column previous lineline
previous line
the code consists of 2 parts:
prev!="" && prev<=prev2 && prev<=$2 {print line}
if
prev
has been assigned value (in other words, if not on first line) , value less bothprev2
, current 2nd column,$2
, know previous line local minimum , print it.prev2=prev; prev=$2; line=$0
here update variables correct when test next line.
local maximum explained
the code local maximum analogous local minimum code except prev
larger either prev2
or $2
.
global minimum
to find line smallest second column:
awk '!m || $2<m {m=$2; line=$0;} end{print line;}' file
global maximum
awk '!m || $2>m {m=$2; line=$0;} end{print line;}' file
Comments
Post a Comment