Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regular expression help

by ghosh123 (Monk)
on Mar 12, 2013 at 08:26 UTC ( [id://1022941]=perlquestion: print w/replies, xml ) Need Help??

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

Hi
I have a file in which I have the following lines alongwith many other lines. How can I grep the following lines for the user, version,server name and date time

For example the users in the following lines are brown, martin, stephen

Server names are elicserv1.muc.ifin.com, elicserv2.muc.ifin.com

Time and date are Mon 3/11 18:31 ...etc

Following are the lines which are to be grepped for the above informations

brown vihl729 172.31.176.71:8532503_6 (v2013.02) (elicserv1.muc.ifin.com/3120 6260), start Mon 3/11 18:31
stephen blrl267 172.20.150.73:233707_1 (v2) (elicserv2.muc.ifin.com/3120 4789), start Tue 3/12 6:32
martin vihlc295 172.31.176.73:33228361_1 (v2012.11) (elicserv1.muc.ifin.com/3120 4152), start Wed 3/6 12:08

Please help.

Replies are listed 'Best First'.
Re: Regular expression help
by McA (Priest) on Mar 12, 2013 at 08:44 UTC

    Hi,

    my quick and dirty proposal:

    #!/usr/bin/perl use strict; use warnings FATAL => qw(all); my @lines = ( 'brown vihl729 172.31.176.71:8532503_6 (v2013.02) (elicserv1.muc.i +fin.com/3120 6260), start Mon 3/11 18:31', 'stephen blrl267 172.20.150.73:233707_1 (v2) (elicserv2.muc.ifin.c +om/3120 4789), start Tue 3/12 6:32 ', 'martin vihlc295 172.31.176.73:33228361_1 (v2012.11) (elicserv1.mu +c.ifin.com/3120 4152), start Wed 3/6 12:08 ', ); foreach my $line (@lines) { my ($user, $d1, $d2, $version, $server, $d3, $s, $date) = split /\ +s+/, $line, 8; for ($version, $server) { s/^\(//; s/\)$//; s#/.+$##; } print "user: $user version: $version server: $server date: $ +date\n"; }

    Best regards
    McA

      This is fine. thanks. But I need it in regular expression.

        But I need it in regular expression..
        So, McA solution is using what? Please show what you have done on your own.
        The result you are getting and what is expected.
        Thanks

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
        "But I need it in regular expression"

        So take this one to continue:

        Update: For some unknown reason i skipped a column, sorry:

        #!/usr/bin/env perl my $line = 'brown vihl729 172.31.176.71:8532503_6 (v2013.02) (elicserv +1.muc.ifin.com/3120 6260), start Mon 3/11 18:31'; $line =~ m/(\w+)\s ([0-9a-z]+)\s [0-9.:()_]+\s \(.+\)\s \(([a-z0-9.]+).+\) ,\s [\w\s]+ (\d+\/\d+\s\d+:\d+) /x; print qq($1 $2 $3 $4\n); __END__ Karls-Mac-mini:monks karl$ ./missing.pl brown vihl729 elicserv1.muc.ifin.com 3/11 18:31

        (If something still makes sense...)

        Karl

        «The Crux of the Biscuit is the Apostrophe»

        What had you tried?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1022941]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found