Raymond gsh
Raymond gsh

Reputation: 383

reading a log file with different sequences using shell script

I'm a starter with shell scripting and I wanted to ask you a question about reading data from a log file. the file is really long and includes few steps of calculation convergence.

step 1

...

converged

final energy : 1000000

step 2

...

converged

final energy : 10000

...

structure optimized

final energy: 100000

what I need to do is to first find if the structure is finally optimized if so the read the final energy and some other data. In mathematica I could find the position of structure optimized and do a search from there is the same thing possible in shell? I'm a starter please list all the commands I need to use

Upvotes: 0

Views: 90

Answers (2)

Shravan Yadav
Shravan Yadav

Reputation: 1317

in awk version

awk '{if($0=="structure optimized")i=1;if(($0~/final energy/) && (i==1)){print "Found optimized "$0;i=0}}' filename

Upvotes: 1

Charles Duffy
Charles Duffy

Reputation: 295308

This might look something like the following:

optimized=0
while IFS= read -r line; do
  case $line in
    "structure optimized")
      optimized=1
      continue
      ;;
    "final energy")
      [ "$optimized" = 1 ] || continue
      echo "Found final energy after structure optimized"
      ;;
  esac
done <input.log

If when you put final energy in your sample file, you really mean something like:

final energy: 10000

...then you might change the relevant clause to:

"final energy: "*)
  [ "$optimized" = 1 ] || continue
  final_energy=${line#*:}
  echo "Found optimized final energy: $final_energy"
  ;;

...but without a detailed and precise specification, how's anyone to know exactly what you mean?

Upvotes: 2

Related Questions