Grep a pattern并忽略其他人

时间:2014-09-02 15:45:42

标签: bash

我有这种模式的输出:

Auxiliary excitation energy for root 3:       (variable value)

输出中出现了一段相关的时间,但我只想看最后一个。 我是bash的初学者,所以我不理解"尾巴"还没有......

这是我写的:

for nn in 0.00000001 0.4 1.0; do
for w in 0.0 0.001 0.01 0.025 0.05 0.075 0.1 0.125 0.15 0.175 0.2 0.225 0.25 0.275 0.3 0.325 0.35 0.375 0.4 0.425 0.45 0.475 0.5; do
a=`grep ' Auxiliary excitation energy for root 3:       ' $nn"_"$w.out`
echo $w"        "${a:47:16}  >> data_$nn.dat
done
done

使用$ nn和$ w参数。

但是这个grep我只有第一个模式。如何才能获得最后一个?

数据示例:

line 1 Auxiliary excitation energy for root 3:     0.75588889  
line 2 Auxiliary excitation energy for root 3:     0.74981555
line 3 Auxiliary excitation energy for root 3:     0.74891111
line 4 Auxiliary excitation energy for root 3:     0.86745155

我的命令grep第1行,我想grep最后一行有我的模式:这是第4行和我的例子。

1 个答案:

答案 0 :(得分:2)

要获得最后一场比赛,您可以使用:

grep ... | tail -n 1

...是你的grep参数。所以你的脚本会读(稍微清理一下):

for nn in 0.00000001 0.4 1.0; do
    for w in 0.0 0.001 0.01 0.025 0.05 0.075 0.1 0.125 0.15 0.175 0.2 0.225 0.25 0.275 0.3 0.325 0.35 0.375 0.4 0.425 0.45 0.475 0.5; do
        a=$( grep ' Auxiliary excitation energy for root 3:       ' $nn"_"$w.out | tail -n 1 )
        echo $w"        "${a:47:16}  >> data_$nn.dat
    done
done