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
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:
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.
The output produced looks like this:
and you may wish to call it something like this from your bash script:
or even:
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
%presenthash; if leaving, it is removed. At the end, the names are listed in order.Is this enough to get you back on track?