简体   繁体   中英

Spotting the first local minimum of 2 column CSV data in a Bash shell script

I want to write a Bash shell script function to return the x, y coordinates of the first local minimum data point in simple 2 column CSV data.

The function would take as an input a Bash variable (say "${myData}") storing data such as the following:

10, 0.14665
20, 0.144971
30, 0.14262
40, 0.142424
50, 0.142370
60, 0.142375
70, 0.142375
80, 0.142375
90, 0.142375
100, 0.142375
110, 0.142306
120, 0.142017
130, 0.141054
140, 0.140148
150, 0.139993
160, 0.139972
170, 0.139958
180, 0.139932
190, 0.139886
200, 0.139876
210, 0.13987
220, 0.139865
230, 0.139861
240, 0.13986
250, 0.139857
260, 0.139855
270, 0.139853
280, 0.139852
290, 0.139847
300, 0.139847

I want the function to spot the first local minimum point (in this case, this would correspond to the coordinate 50, 0.142370) and return the coordinate of this point. Could you suggest a simple way of doing this?

You can use awk, either on one line or prettily indented as in here:

awk '
  NR > 1 { 
    if ($2 > n) { 
      print line; 
      exit(0); 
    } 
  }  
  { 
    line=$0; 
    n=$2 
  }
' <<< "${myData}"

You can also take out the exit(0); to show all local minimas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM