stevbutt has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I am loading some log files and need to extract the username when it appears, the problem is there is some variety in the way it is reported

What I have been trying is this

my ( $user ) = $remainder =~ m/user=\s*(\S+)/;

my string contained in remainder can contain the user in the following formats

.... user=jtkirk, ip=[::ffff:127.0.0.1], top=0,.... .... user=<jtkirk@enterprise.com>, ip=[::ffff:127.0.0.1], top=0,.... .... user="jtkirk@enterprise.com" passwd="XXXXXX"....

Just to be clear that when the username takes the form of an email address I do want to capture the whole address but dont want the quotes or > < signs

Do I need to capture the whole thing then strip the quotes etc with tr ?

Replies are listed 'Best First'.
Re: Getting my username from log
by kcott (Archbishop) on Aug 15, 2012 at 18:51 UTC

    This works with the data you provided:

    #!/usr/bin/env perl use 5.010; use strict; use warnings; my $re = qr{ user= [<"]? ( \S+? ) [>"]? ,? \s }x; while (<DATA>) { /$re/; say $1; } __DATA__ .... user=jtkirk, ip=[::ffff:127.0.0.1], top=0,.... .... user=<jtkirk@enterprise.com>, ip=[::ffff:127.0.0.1], top=0,.... .... user="jtkirk@enterprise.com" passwd="XXXXXX"....

    Output:

    $ pm_log_uname_regex.pl jtkirk jtkirk@enterprise.com jtkirk@enterprise.com

    -- Ken

Re: Getting my username from log
by NetWallah (Canon) on Aug 15, 2012 at 16:17 UTC
    Try this regex:
    m/\s+user=\W([\w\@\.]+)/
    However, Regexp::Common::Email::Address has much better built-in scanning for email addresses, and I'd recommend you use that.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Thanks NetWallah

      This works for two of the cases but not the one where the username is not an email address i.e. doesnt contain an @

        ....Also if the email is user=jtkirk@starship-enterprise.com then I end up with jtkirk@starship

        Steve

Re: Getting my username from log
by aitap (Curate) on Aug 15, 2012 at 17:00 UTC
    Try this:
    my $user = $2 if $remainder =~ m/ user= (\W?) # possible one non-word character ([\w\-.@]+) # word characters and -.@ # there should be a way to improve it, # some kind of [^\g1] \g1 /x;
    Sorry if my advice was wrong.