2014-09-02 91 views
0

我有这种模式的输出:grep的模式,而忽略其他

Auxiliary excitation energy for root 3:  (variable value) 

这似乎时间在输出结果的数字,但我只希望到grep最后一个。 我在bash初学者,所以我不明白的“尾巴” fonction尚未...

这里是我写的:

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。

+0

对不起,你不清楚你需要什么。你能否用多行重新提供提供样本数据的问题,并准确指定要捕获的内容? – theMarceloR 2014-09-02 15:51:43

+0

@theMarceloR:我添加一个数据的例子,我希望这会有所帮助。 – 2014-09-03 11:00:25

回答

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 
+0

它运作良好! – 2014-09-03 11:03:26

+0

@B_runo,如果这是答案,你能接受吗? – zerodiff 2014-09-03 12:59:15