tags:

views:

61

answers:

3

I'm attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined). Note that Samual has rejoined.

[Sun Nov 15 14:12:50 2009] [GAME: Tower Defense Join Fast!] player [Samual|192.168.1.115] joined the game
[Sun Nov 15 14:12:54 2009] [GAME: Tower Defense Join Fast!] deleting player [Samual]: has left the game voluntarily
[Sun Nov 15 14:12:57 2009] [GAME: Tower Defense Join Fast!] player [Jack|192.168.1.121] joined the game
[Sun Nov 15 14:13:04 2009] [GAME: Tower Defense Join Fast!] player [NoobLand|192.168.1.153] is trying to join the game but is banned by IP address
[Sun Nov 15 14:13:04 2009] [GAME: Tower Defense Join Fast!] [Local]: User [NoobLand] was banned on server [www.example.com] on 2009-11-04 by [Owner] because [].
[Sun Nov 15 14:13:13 2009] [GAME: Tower Defense Join Fast!] player [Jones|192.168.1.178] joined the game
[Sun Nov 15 14:13:21 2009] [GAME: Tower Defense Join Fast!] player [Arnold|192.168.1.126] joined the game
[Sun Nov 15 14:13:35 2009] [GAME: Tower Defense Join Fast!] [Local]: Autokicking player [Arnold] for excessive ping of 131.
[Sun Nov 15 14:13:35 2009] [GAME: Tower Defense Join Fast!] deleting player [Arnold]: was autokicked for excessive ping of 131
[Sun Nov 15 14:13:44 2009] [GAME: Tower Defense Join Fast!] [Lobby] [Jones]: !chekme
[Sun Nov 15 14:13:44 2009] [GAME: Tower Defense Join Fast!] non-spoofchecked user [Jones] sent command [chekme] with payload []
[Sun Nov 15 14:13:45 2009] [GAME: Tower Defense Join Fast!] [Local]: Waiting for 4 more players before the game will automatically start.
[Sun Nov 15 14:14:05 2009] [GAME: Tower Defense Join Fast!] player [Samual|192.168.1.116] joined the game

To give me a list of players currently in the room like this (I can probably use tr for case switch):

'jack','jones','samual'

When a player joins it will say "player [Playername|PlayerIP] joined the game" (you can ignore the IP. When a player leaves it will say "deleting player [Playername]: Some Reason

While I know how to get at both of these lists individually, I need to combine them to somehow figure out who is still in the room, and order is important because they can rejoin. Can anyone help me with this?

I have this sed statement to give me the players who have joined:

sed -n 's/\[.*\] \[GAME: .*\] player \[\(.*\)|.*\] joined the game/\1/p`

and this to give me a list of leavers:

sed -n 's/\[.*\] \[GAME: .*\] deleting player \[\(.*\)\].*/\1/p'

But don't know how to combine the two, or to put the ticks and commas in the list of players.

Thanks

+3  A: 

Part of solving a problem effectively is choosing the right tool. In this case, trying to do it all in bash is probably a worse choice than something like Perl or Python, because:

  • it will be more complicated to implement, debug and maintain
  • it will use many more processes
  • the performance is likely to be poor

If you would like help to write this in a more appropriate language, just ask.

…later…

OK, you chose Perl….

You could certainly do everything which you're trying to do in Perl itself. However, as you're unfamiliar with it, I've put together a little toy program which should do what you're after.

#!/usr/bin/perl -w

use strict;

my %present;

while (<>) {  # loop over input lines
  if (/player \[(.*?)(?:\|\d+\.\d+\.\d+\.\d+)?\]:? (.*)/) {
    my $player = $1;
    my $event = $2;

    if ($event eq "joined the game") {
      $present{$player} = 1;
    } elsif ($event eq "has left the game voluntarily") {
      delete $present{$player};
    } elsif ($event =~ /^was autokicked/) {
      delete $present{$player};
    }
  }
}

foreach (sort keys %present) {
  print "$_\n";
}

The output produced looks like this:

$ ./analyse inputfile
Jack
Jones
Samual

and you may wish to call it something like this from your bash script:

tail -1000 ghost.log | ./analyse

or even:

playerspresent=`tail -1000 ghost.log | ./analyse`

I've tried to keep the Perl program as simple as is sensible. The only "difficult" bit is the regular expression. Essentially, it loops over the lines of input, trying to decide whether each line represents someone joining or leaving. If joining, the username is added to the %present hash; if leaving, it is removed. At the end, the names are listed in order.

Is this enough to get you back on track?

Tim
Perl would work, but I don't have any experience with it so would need a lot of help. The current form of the script grabs the last 1000 lines of an active log file called ghost.log. Then it finds the most recent game that I "Owner" joined, and filters it down to just a list of items related to that gamename. Then I want a list of players currently in the game than I am in. From there I need to query my sqlite3 database where the list of names is one of the "where" statements. Should I use perl for all this? Or have bash call a seperate perl script to do the parsing? How would I do this?
Dan
I've been able to read most of this. I understand most of the regex too... but what is the :? and the :?. Why is the middle parathesis not $2, and the if statement is true for lines that contain that? What does the first (/ and the last /) represent?
Dan
OK, the first `?:` is part of a `(?: … )` construct. This does clustering, rather than capturing. That means that the contents are treated as a whole, but don't get assigned to the `$2` variable. The reason for this is that we don't care what the content is, but we do want the following `?` sign to operate on the whole chunk. When you put a `?` after group or a single character, that says that the group or character is optional. Here we use it to say that wn IP address (the thing in the `(?: … )` construct) can be present or absent. Also, the `:?` later on: a colon may or may not be present.
Tim
+1  A: 

This might work for what you want. It assumes, probably unsafely, that if a player is listed an odd number of times in the combined joined and left lists that he is currently in the game (e.g. join or join-leave-join) while an even number of entries indicates that he has left (e.g. join-leave).

#!/bin/bash
joined=$(sed -n 's/\[.*\] \[GAME: .*\] player \[\(.*\)|.*\] joined the game/\1/p' gamefile.txt)

left=$(sed -n 's/\[.*\] \[GAME: .*\] deleting player \[\(.*\)\].*/\1/p' gamefile.txt)

saveIFS="$IFS"
IFS=$'\n'
players=("$(echo -e "$joined\n$left" | sort | uniq -c)")
IFS="$saveIFS"

flag=0
echo -n "'"
for i in "${players[@]}"
do
    if [[ $flag == 1 ]]
    then
        echo -n "', '"
    fi
    j=($i)
    if (( ${j[0]}%2 == 1 ))
    then
        flag=1
        echo -n "${j[1]}"
    fi
done
echo "'"

Result from your example data:

'Jack', 'Jones', 'Samual'
Dennis Williamson
+1  A: 

you can use gawk

awk '$11=="player" && ( $(NF-2)=="joined" || $12=="[Lobby]" ) {
 gsub(/\[|\|.*\]|:/,"",$12) ; player[$12]
}
$11=="deleting"{gsub(/\[|\]|:/,"",$13); delete player[$13]  }
END{
 for(i in player){  print i }
}' file

output

./shell.sh
Jones
Samual
Jack
ghostdog74