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:

  1. could explain above codes shortly? used , saw works, don't know how works...

  2. 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 ago

  • prev value of second column previous line

  • line previous line

the code consists of 2 parts:

  1. prev!="" && prev<=prev2 && prev<=$2 {print line}

    if prev has been assigned value (in other words, if not on first line) , value less both prev2 , current 2nd column, $2, know previous line local minimum , print it.

  2. 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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -