This is extended question from https://stackoverflow.com/questions/32081168/reading-a-log-file-with-different-sequences-using-shell-scriptI'm new to shell. I've written a shell script a code with finds a sentence I want to subtract the value from that sentence. the sentence is as belowFINAL SINGLE POINT ENERGY       -39.022584378179I want the numerical part, expr substr gives me unknown command error and actually the lenght of my numerical part may vary so I wanted to use awk something like:awk -F  '{ print $1 }' $linecan you guys help mehere's my codeoptimized=0while IFS= read -r line; do  case $line in    *HURRAY*)      optimized=1      continue      ;;    FINAL SINGLE POINT ENERGY      *)      [ $optimized = 1 ] || continue      final_energy=`echo $line | awk '{print $NF }'`      echo Found optimized final energy: $final_energy      ;;    final energy)  esacdone < input-s.outthe log file is a long file contains many FINAL SINGLE POINT ENERGY and has to read only the last one which comes after hurray.
Subtract a Number from variable which contains a sentence
shell;awk;string
$ awk '/HURRAY/ { ok = 1 } ok == 1 && /^FINAL SINGLE POINT ENERGY/ { printf(Found optimized final energy: %s\n, $NF) }' input-s.outI think this does what your shell script is trying to do.If it finds the string HURRAY somewhere, it sets ok to 1 (your optimized variable).  If ok is 1 and it finds a line starting with the string FINAL SINGLE POINT ENERGY, it displays the message followed by the value in the last column of that same line.