user4914499
user4914499

Reputation: 354

awk script to find local minimum and local maximum from 2 clomun data

I want to know simple implementation of finding local extreme from a datafile.

Specifically saying,

My input data is:

 x       y
 163.858 14.0919
 166.269 14.2113
 168.688 14.2855  # local max.
 171.109 14.2766
 173.524 14.1439
 178.353 13.7528  
 180.768 13.6166
 183.189 13.5753 # local min.
 185.61 13.6128
 188.03 13.6525
 190.454 13.776
 195.281 14.1291
 197.698 14.1603 # local max.
 200.119 14.1161

which has 3 extreme (2 for local max. and 1 for local min) as clearly seen in the attached viewgraph. ![enter image description here][1]

Anyone would be appreciated if they teach me awk(or shell) script which output local extremes.

Thanks in advance.

Upvotes: 0

Views: 533

Answers (1)

user000001
user000001

Reputation: 33387

One approach would be to store the previous two values lines in two variables, and use a third variable to store the line. So you could get the local minimum like this:

awk 'prev!=""&&prev<=prev2&&prev<=$2{print line}{prev2=prev;prev=$2;line=$0}' file

and the local maximum like this:

awk 'prev!=""&&prev>=prev2&&prev>=$2{print line}{prev2=prev;prev=$2;line=$0}' file

Upvotes: 1

Related Questions