grabing a number six lines below a pattern

倾然丶 夕夏残阳落幕 提交于 2019-12-01 23:07:59

问题


I have these lines repeating

                               FINAL RESULTS



    NSTEP       ENERGY          RMS            GMAX         NAME    NUMBER
    1000      -4.7910E+01     2.1328E-01     9.4193E-01     C          62

The FINAL RESULTS indicate a average of those values for a set. The output file combines all 1000 sets. I need to grab the number below energy (-4.7910E+01), all 1000 of them in to another file. I need to set FINAL RESULTS as a pattern because other pattern such as NSTEP, ENERGY, RMS.... are repeated in millions.

I'll be grateful for any help.


回答1:


Something like this should work for you:

awk '/FINAL RESULTS/{for (i=0; i<5; i++) getline; print $2}' <filename>



回答2:


OK, I think I see now.

awk 'found==1 { print $2; found=0 } $2=="ENERGY" { found=1 }' inputfile

This will get the number below ENERGY regardless of how many lines there are between it and FINAL RESULTS.




回答3:


This might work for you:

awk '/FINAL RESULTS/{f=1;p=0}{p++}f==1 && p==6{print $2}' file

Or if you like:

awk 'f==2 && p==1{print $2}{p++}/FINAL RESULTS/{f=1}/ENERGY/ && f==1{f=2;p=0}' file



回答4:


With GNU grep (and some others following its extensions) you could just do something like grep -A 6 'FINAL RESULTS'

So that should work for Linux, and I see that it works on a recent MacOS X and I'm pretty sure I've used it on FreeBSD. So it probably works on most forms of UNIX you're going to encounter. (If not, install the GNU "coreutils" package).



来源:https://stackoverflow.com/questions/9040890/grabing-a-number-six-lines-below-a-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!