2013-07-20 78 views
0

我在尋找的東西是在我的世界記錄玩家死亡的消息。BASH:尾礦日誌文件到行相匹配的陣列

所以我的劇本正在觀看的Minecraft的server.log,我已經創建了所有可能的死亡消息的數組,但它只是不工作,因爲它在輸出不斷線出現在日誌中。下的server.log的

#!/bin/bash 

serverlog=/home/skay/NewWorld/server.log 
outputfile=/home/skay/website/log/playerstats.log 


##  File Creation! 
if [ ! -f "$outputfile" ]; then 
    touch $outputfile 
    echo "file created"; else 
    echo "file already existed" 
fi 

echo "Starting Loop" 

while true; do 

index="0" 
newline=`tail -n 1 "$serverlog"` 
joined=`tail -n 1 $serverlog | grep "[INFO]" | grep "joined the game"` 
left=`tail -n 1 $serverlog | grep "[INFO]" | grep "left the game"` 

##  Death Message Array 
deathmsg=( "was squashed by a falling anvil" 
      "was pricked to death" 
      "walked into a cactus whilst trying to escape" 
      "was shot by arrow" 
      "drowned" 
      "blew up" 
      "was blown up by" 
      "hit the ground too hard" 
      "fell from a high place" 
      "fell off a ladder" 
      "fell off some vines" 
      "fell out of the water" 
      "fell into a patch of fire" 
      "fell into a patch of cacti" 
      "was doomed to fall" 
      "was shot off some vines by" 
      "was shot off a ladder by" 
      "was blown from a high place by" 
      "went up in flames" 
      "burned to death" 
      "was burnt to a crisp whilst fighting" 
      "walked into a fire whilst fighting" 
      "was slain by" 
      "was shot by" 
      "was fireballed by" 
      "was killed by" 
      "got finished off by" 
      "was slain by" 
      "tried to swim in lava" 
      "died" 
      "got finished off by" 
      "was slain by" 
      "was shot by" 
      "was killed by" 
      "was killed by magic" 
      "starved to death" 
      "suffocated in a wall" 
      "was killed while trying to hurt" 
      "fell out of the world" 
      "fell from a high place and fell out of the world" 
      "was knocked into the void by" 
      "withered away") 


##  Player Joined 
    if [ "$newline" != "$oldline" ]; then 
     if [ "$newline" == "$joined" ]; then 
      echo "$joined" 
      oldline="$newline" 
     fi 

##  Player Disconnected 
     if [ "$newline" == "$left" ]; then 
      echo "$left" 
      oldline="$newline" 
     fi 

##  Player Death Message 
     while [ "$index" -le "42" ]; do 
      death=`tail -n 1 $serverlog | grep "[INFO]" | grep "${deathmsg[$index]}"` 
      if [ "$newline" == "$death" ]; then 
       echo "$death" 
       oldline="$death" 
      fi 
      index=$[$index+1] 
     done 
    fi 
done 

例子:

2013-07-21 00:38:36 [SEVERE] Reached end of stream for /79.97.91.46 
2013-07-21 00:38:38 [INFO] Fenlig[/79.97.91.46:59709] logged in with entity id 880461 at (541.34081297678, 48.0, 463.3734054913931) 
2013-07-21 00:38:38 [INFO] Fenlig joined the game 
Player Fenlig login detected 
2013-07-21 00:39:49 [INFO] Fenlig was doomed to fall 
2013-07-21 00:39:57 [INFO] Fenlig lost connection: disconnect.quitting 
2013-07-21 00:39:57 [INFO] Fenlig left the game 
+1

什麼是你的問題?日誌文件是什麼樣的? – franklin

+0

@franklin 以下是日誌示例 '2013-07-21 00:38:36 [SEVERE]已到達流結束位置/79.97.91.46 2013-07-21 00:38:38 [信息] Fenlig [/79.97.91.46:59709]用實體ID 880461登錄到(541.34081297678,48.0,463.3734054913931) 2013-07-21 00:38:38 [INFO] Fenlig加入遊戲 2013-07-21 00:39 :49 [INFO] Fenlig註定要落在 2013年7月21日0時39分57秒[INFO] Fenlig失去連接:disconnect.quitting 2013年7月21日0時39分57秒[INFO] Fenlig離開game' –

+0

您是否試圖找出問題所在?例如:哪個'echo'語句打印出你不打算打印的行?如果是'echo'$ death''語句,那麼'$ index'是問題所在? – ruakh

回答

1

@Andy答案是一個更好的方式去;你也可以用bash做類似的事情。

但是,爲了回答直接的問題,我覺得你的問題是,你越來越指數高達42,而

$ echo ${deathmsg[42]} 

$ 

由於數組索引從0開始,它結束與41 42空白,然後在死亡=行匹配上的最後一個grep。

0

你有沒有想過用AWK?寫這篇文章到parse_log.awk

{ 
    /* only process [INFO] messages */ 
    if ($3 == "[INFO]") { 
    rest_of_string = substr($0, index($0, $4)); 

    /* logged in, and anything else you want to skip ... */ 
    if ($5 == "logged") { 
     next; 
    } 

    /* joined ... */ 
    if ($5 == "joined") { 
     print rest_of_string; 
     next; 
    } 

    /* left ... */ 
    if ($5 == "left") { 
     print rest_of_string; 
     next; 
    } 

    /* otherwise death */ 
    print rest_of_string; 
    } 
} 

然後在命令行中,你可以簡單地運行這個

$尾-n 1 -f /home/skay/website/log/playerstats.log | awk -f parse_log.awk >> /home/skay/website/log/playerstats.log

實質上,tail保持跟蹤日誌(-f),然後將它傳遞給你的awk程序,如果它產生了輸出,其附加到文件