This is my file:print energy 1 end  Local net energy (PE 0) = 1.635393034217E+05 kJ/mol  Global net ELEC energy = 1.635393034217E+05 kJ/molprint energy 2 end  Local net energy (PE 0) = 1.682997967326E+05 kJ/mol  Global net ELEC energy = 1.682997967326E+05 kJ/molI need the numbers, but the only way I know which one for me is the print line. So far, I have used this command to work it out:tail -16 file.txt | head -1 | awk '{print $6}'But this way is not good I believe. You have to rely on the line number. If the data is in the middle of thousand lines, you are lost. Do you know any better choice for this?
Getting data under or above a line
text processing;grep
Assuming you want to print the 6th field of the second line after each one containing print (guessing), you can use either:awk '/print/{if (getline > 0 && getline > 0) print $6}'Or:awk '!--n {print $6}; /print/ {n=2}'If you want the but-last field in the first line containing Global that occurs after the first occurrence of print energy 1 end, that would be:awk '/print energy 1 end/{found=1}; found && /Global/ {print $(NF-1); exit}'To print data from lines above the match, you'll need to record data first and then print the recorded data upon the match. For instance to print the 6th field from the line before the first one that contains print:awk '/print/{print recorded;exit};{recorded=$6}'