min and max in certain lines of input file with th

2019-09-21 01:34发布

问题:

I want to find the minimum between the line number 2000 and 3000.

I want to find the minimum and maximum of the values and when the minimum and maximum are found , I also need to find the line on which it was found. Then I want to go to one line above the maximum or minimum containing line and output the first integer out of the 6 integers on the same line.

My input is of the following form.

*KEYWORD
$TIME_VALUE = 1.4000002e+001
$STATE_NO = 15
$Output for State 15 at time = 14
*ELEMENT_SHELL_THICKNESS
1346995      25 1457683 1471891 1457727 1471929
9.953265e-001   9.953265e-001   9.953265e-001   9.953265e-001
1346996      25 1471891 1457685 1471930 1457727
9.953963e-001   9.953963e-001   9.953963e-001   9.953963e-001
1346997      25 1457685 1471892 1471931 1471930
9.953437e-001   9.953437e-001   9.953437e-001   9.953437e-001

so output could be

min=9.953265e-001  on line  07   at  1346995
max=9.953963e-001  on line  09   at  1346996

PS: I can find the min and maximum of an array but to tackle this input is hard for me. waiting for expert suggestion.

回答1:

I dot not understand the requirement between line number 2000 and 3000, but if it is in your example between line number 6 and 11:

cat your_file | awk '
  NR >= 6 && NR <= 11{at=$1;getline
    if (max < $1){max=$1;max_line=NR;max_at=at}
    if (min > $1){min=$1;min_line=NR;min_at=at}}
  NR == 7{min=$1;min_line=NR;min_at=at}
  END{
    printf "min=%-13e on line  %02d at %8d\n", min, min_line, min_at
    printf "max=%-13e on line  %02d at %8d\n", max, max_line, max_at}'

And would the max not be on line 9 ?

(To all, please do not useless use of cat me, I find it clearer this way ;-).)