2014-09-02 90 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