grep the final,而不是第一次出现一行

时间:2018-04-13 07:06:56

标签: linux grep

我正在寻找一种方法来grep最后一次出现的一行,例如运行

$grep "!" ibrav8_c11mc12/* 

返回:

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

但我只想要每个文件的最后一次出现,有一个-m选项,

grep "!"  -m 1 ibrav8_c11mc12/* 

但是这会从每个文件返回第一个匹配项。我想要的,基本上是这个选项的相反

1 个答案:

答案 0 :(得分:0)

我的答案与get last line from grep search on multiple files几乎完全相同,只是我需要稍微调整一下以打印出文件名:

for i in ibrav8_c11mc12 / * out;做GREP_RES = $(grep!$ f | tail -1); echo $ f $ GREP_RES;完成