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:
prev2value of second column 2 lines agoprevvalue of second column previous linelineprevious line
the code consists of 2 parts:
prev!="" && prev<=prev2 && prev<=$2 {print line}if
prevhas 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=$0here 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