Monday, November 1, 2021

[SOLVED] How to extract data in such a pattern using grep or awk?

Issue

I have multiple instances of the following pattern in my document:

Dipole Moment: [D]
     X:     1.5279      Y:     0.1415      Z:     0.1694     Total:     1.5438

I want to extract the total dipole moment, so 1.5438. How can I pull this off?

When I throw in grep "Dipole Moment: [D]" filename, I don't get the line after. I am new to these command line interfaces. Any help you can provide would be greatly appreciated.


Solution

Could you please try following. Written and tested with shown samples in GNU awk.

awk '/Dipole Moment: \[D\]/{found=1;next} found{print $NF;found=""}' Input_file

Explanation: Adding detailed explanation for above.

awk '                     ##Starting awk program from here.
/Dipole Moment: \[D\]/{   ##Checking if line contains Dipole Moment: \[D\] escaped [ and ] here.
  found=1                 ##Setting found to 1 here.
  next                    ##next will skip all further statements from here.
}
found{                    ##Checking condition if found is NOT NULL then do following.
  print $NF               ##Printing last field of current line here.
  found=""                ##Nullifying found here.
}
' Input_file              ##Mentioning Input_file name here.


Answered By - RavinderSingh13