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

I am trying to split a log line that looks something like this.
Aug 1 12:12:12 host [login=name, pid=1356] -> sh [args: "sh", "-c", "l +s -l", "/var/log"]
I am having a hard time getting the information from within the second set of braces [args: ..]. The items in the "" are not always a fix number of set (meaning that there can be only two things in with quotes or many more things with the quotes.) I want to get the information that is after the args: into a variable without the quotes and commas. Can anyone help?

Fixed square bracket - dvergin 2003-08-01

Replies are listed 'Best First'.
Re: Splitting on an undefined # of variables
by sgifford (Prior) on Aug 01, 2003 at 21:51 UTC
    Here's an example of how to do this using split.
    #!/usr/bin/perl -w use strict; my $line = 'Aug 1 12:12:12 host [login=name, pid=1356] -> sh [args: "s +h", "-c", "ls -l", "/var/log"]'; # First pull out the arguments, with first # and last quotes stripped. $line =~ /sh \[args: "(.*?)"\]/; my $cmdline = $1; print "Command: $cmdline\n"; # Now split apart on the quotes and commas. my @args = split(/", "/,$cmdline); print "Args are: **",join("**, **",@args),"**\n";
Re: Splitting on an undefined # of variables (golf)
by BrowserUk (Patriarch) on Aug 01, 2003 at 22:15 UTC

    For fun.

    $s = 'Aug 1 12:12:12 host [login=name, pid=1356] -> sh [args: "sh", "- +c", "ls -l", "/var/log"]'; $s =~ m{\[args:\s+"(.*)"\]} and @args = split '",\s+"', $1; print $_, $/ for @args; sh -c ls -l /var/log

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Splitting on an undefined # of variables
by dga (Hermit) on Aug 01, 2003 at 21:50 UTC

    Perhaps something along the lines of:

    while(<>) { my(undef, $args)=split 'args:'; # clean up the $args string here # ie chop off the ], clean up the quotes etc. # here you are ready to do your processing }