0

I"m looking for a way to grep the last occurrence of a line, e.g. running

$grep "!" ibrav8_c11mc12/* 

returns:

ibrav8_c11mc12/MgO.scf.a=0.090.ecut=80.k=4.out:!    total energy              =    -608.35146103 Ry
ibrav8_c11mc12/MgO.scf.a=0.090.ecut=80.k=4.out:!    total energy              =    -608.35234197 Ry
ibrav8_c11mc12/MgO.scf.a=0.090.ecut=80.k=4.out:!    total energy              =    -608.35243673 Ry
ibrav8_c11mc12/MgO.scf.a=0.095.ecut=80.k=4.out:!    total energy              =    -607.63532091 Ry
ibrav8_c11mc12/MgO.scf.a=0.095.ecut=80.k=4.out:!    total energy              =    -608.28250682 Ry
ibrav8_c11mc12/MgO.scf.a=0.095.ecut=80.k=4.out:!    total energy              =    -608.33000791 Ry
ibrav8_c11mc12/MgO.scf.a=0.095.ecut=80.k=4.out:!    total energy              =    -608.34213906 Ry
ibrav8_c11mc12/MgO.scf.a=0.095.ecut=80.k=4.out:!    total energy              =    -608.34395220 Ry
ibrav8_c11mc12/MgO.scf.a=0.095.ecut=80.k=4.out:!    total energy              =    -608.34488323 Ry
ibrav8_c11mc12/MgO.scf.a=0.095.ecut=80.k=4.out:!    total energy              =    -608.34496519 Ry
ibrav8_c11mc12/MgO.scf.a=0.100.ecut=80.k=4.out:!    total energy              =    -607.48596003 Ry
ibrav8_c11mc12/MgO.scf.a=0.100.ecut=80.k=4.out:!    total energy              =    -608.28313222 Ry
ibrav8_c11mc12/MgO.scf.a=0.100.ecut=80.k=4.out:!    total energy              =    -608.32079049 Ry
ibrav8_c11mc12/MgO.scf.a=0.100.ecut=80.k=4.out:!    total energy              =    -608.33433606 Ry
ibrav8_c11mc12/MgO.scf.a=0.100.ecut=80.k=4.out:!    total energy              =    -608.33598793 Ry
ibrav8_c11mc12/MgO.scf.a=0.100.ecut=80.k=4.out:!    total energy              =    -608.33685159 Ry
ibrav8_c11mc12/MgO.scf.a=0.100.ecut=80.k=4.out:!    total energy              =    -608.33690888 Ry

But I just want the last occurrence from each file, there is a -m option,

grep "!"  -m 1 ibrav8_c11mc12/* 

But this returns the first occurrence from each file. What I want, essentially is the opposite of this option.

Daniel Marchand
  • 584
  • 8
  • 26
  • Ideally, but not necessarily. I use this in a more complex command: grep ! $1/* | perl -lne 'print join " ", /\d+(?:\.\d+)?/g' > $ENERGY_RESULTS, which I would rather preserve as much as possible – Daniel Marchand Apr 13 '18 at 07:13
  • @Daniel could you clarify - do you need last match from each input file or just last match from grep result? – Sundeep Apr 13 '18 at 07:13
  • @Sundeep I need the last match from each input file. To give some background, each input file represents a different physical configuration. Within each input file, there is a calculation that occasionally dumps "the energy" of the system. I am only interested in the final energy dump per input file. – Daniel Marchand Apr 13 '18 at 07:29

1 Answers1

0

My answer was almost exactly the same as get last line from grep search on multiple files except I needed to tweak things slightly to print out the file name:

for f in ibrav8_c11mc12/*out; do GREP_RES=$(grep ! $f | tail -1); echo $f $GREP_RES; done

Daniel Marchand
  • 584
  • 8
  • 26