1

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/mol

print energy 2 end
  Local net energy (PE 0) = 1.682997967326E+05 kJ/mol
  Global net ELEC energy = 1.682997967326E+05 kJ/mol

I 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?

  • so in a thousands of lines, print appears for only two times. Am i correct? – Avinash Raj Jun 18 '14 at 11:44
  • No, it can appear many times. – Ooker Jun 18 '14 at 12:03
  • it's unclear for me. Did you want to get all the numbers which are just below to the print line? – Avinash Raj Jun 18 '14 at 12:05
  • I just to have the number in 'Global' line – Ooker Jun 18 '14 at 12:08
  • so want to print the number from the global line which must be below(as second) to the print line. – Avinash Raj Jun 18 '14 at 12:10
4

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}'
  • The last one is what I need. What if I need the data above that? Thank you. – Ooker May 29 '14 at 19:49
1

With your sample data, here is a GNU grep solution:

$ grep -oP '(?<== ).+? ' file
1.635393034217E+05 
1.635393034217E+05 
1.682997967326E+05 
1.682997967326E+05
  • 1
    This only works for the sample data, my real file is more complicate than that. Thank you. – Ooker May 29 '14 at 19:48
1

You could use cut as well:

cut -d "=" -f2 file

And if you don't want the "kJ/mol" then pipe with awk:

cut -d "=" -f2 file | awk '{print $1}'
  • 1
    This only works for the sample data, my real file is more complicate than that. Thank you. – Ooker May 29 '14 at 19:47
1

I assumed all the print statements are separated by a blank line and also i assumed that you want to print the 6th field of the second line after each one containing print.

$ cat ccc
print energy 1 end
  Local net energy (PE 0) = 1.635393034217E+05 kJ/mol
  Global net ELEC energy = 1.635393034217E+05 kJ/mol

print energy 2 end
  Local net energy (PE 0) = 1.682997967326E+05 kJ/mol
  Global net ELEC energy = 1.682997967326E+05 kJ/mol

Read energy 3 end
  Local net energy (PE 0) = 1.635393034217E+05 kJ/mol
  Global net ELEC energy = 1.635393034217E+05 kJ/mol


$ awk -v RS='\n\n' '/print/{gsub (/\n/,""); {print $18}}' ccc
1.635393034217E+05
1.682997967326E+05
  • I don't get it. What is the purpose of duplicate the energy 1? – Ooker May 31 '14 at 0:03
  • I don't even get your question. – Avinash Raj May 31 '14 at 2:36
  • Which one? The one I initially ask or the one on the comment above? And what point you don't get it? – Ooker May 31 '14 at 2:41
  • is this "I assumed all the print statements are separated by a blank line and also i assumed that you want to print the 6th field of the second line after each one containing print." assumption correct? – Avinash Raj May 31 '14 at 3:00
  • Yes, that's right. Oh I get it, did you just use the old data from 1 for the example, the data in 3 was something added to be looked like a real file? – Ooker Jun 18 '14 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.