Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

linux - min and max in certain lines of input file with the tag of line number

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", min, min_line, min_at
    printf "max=%-13e on line  %02d at %8d
", 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 ;-).)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...