English 中文(简体)
Parse players currently in lobby
原标题:

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:

  • 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 "$_
";
}

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?

问题回答

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=$ 
 
players=("$(echo -e "$joined
$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 

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




相关问题
Parse players currently in lobby

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)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签