2016-02-08 60 views
0

系統:AIX 7.1grep的最新文件字符串

比方說,我有一個文件夾中包含這些文件:

enter image description here

的文件,我需要一直有這個前綴sapemea_postatus.log。 xxxxxxxx,其中我們總是有一個數字(集合號碼),像這樣:

enter image description here

而且我一直有一個像

嘿問題,這集數是最後一個,我們有嗎?

所以我總是要打開FTP,瀏覽到該文件夾​​,過濾欄,顯示在頂部的最新的,看看最新的sapemea_postatus.log文件,下載並用記事本查看++。

所以我的問題是:有沒有辦法讓grep命令在那裏我可以過濾這些文件,獲取最近的一個,並顯示這個集合號?謝謝!

+0

ls -tr> FILES; cat \'tail -1 FILES \'| grep ... –

回答

1
#!/bin/bash 

latest_collection=0 

regex="Collection:[ ]*([0-9]+)" 

for log in sapemea_postatus.log.*; do 

    [[ $(cat $log) =~ $regex ]] 
    collection="${BASH_REMATCH[1]}" 

    if [[ $collection > $latest_collection ]]; then 
     latest_collection=$collection 
     latest_log=$log 
    fi 
done 

echo "Latest log: $latest_log (number $latest_collection)" 
+0

謝謝,我會檢查我是否可以在AIX服務器中創建一個文件(如果他們讓我)並嘗試這個! – sanjuro8998

+0

如果服務不會讓你創建文件,那麼你實際上可以將腳本作爲單線程運行,因爲縮進在Bash中沒有意義。如果不是系統的默認shell,只需啓動'bash',然後執行'latest_collection = 0; regex =「Collection:[] *([0-9] +)」;用於登錄sapemea_postatus.log。*; do [[$(cat $ log)=〜$ regex]];收集= 「$ {BASH_REMATCH [1]}」;如果[[$ collection> $ latest_collection]];那麼latest_collection = $ collection; latest_log = $登錄;網絡連接;完成;回聲「最新日誌:$ latest_log(number $ latest_collection)」' –

+0

我寫了bash,然後回車,並粘貼了所有行......如何現在執行它?對不起,我真的是aix的初學者 – sanjuro8998