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

I am an intermittent perl user
I have redirected the output of command to a scalar variable
 my $msl=`mt schedule -info` or die "Cannot find mt: $!"; the actual output of the command is of the format
task.start # task.id: 1 task.name:"abc" task.info: xyz task.schedule: task.notify: task.nextruntime: task.lastmodifiedtime: 2010-04-12T11:57:56+01:00 by user1@server1 task.args:
I just want print out the task.name and task.modifiedtime
from $msl variable , what are the possible ways in perl ?

Replies are listed 'Best First'.
Re: Nested search and print
by toolic (Bishop) on Apr 12, 2010 at 16:22 UTC
    I just want print out the task.name and task.modifiedtime from $msl variable , what are the possible ways in perl ?
    I assume "modifiedtime" is a typo and you really meant "lastmodifiedtime". One way is to use regular expressions to capture just the info you desire:
    use strict; use warnings; my $msl = ' task.start # task.id: 1 task.name:"abc" task.info: xyz task.schedule: task.notify: task.nextruntime: task.lastmodifiedtime: 2010-04-12T11:57:56+01:00 by user1@server1 '; my ($name) = $msl =~ /task\.name:(.*)/; print "$name\n"; my ($time) = $msl =~ /task\.lastmodifiedtime:(.*)/; print "$time\n"; __END__ "abc" 2010-04-12T11:57:56+01:00 by user1@server1
Re: Nested search and print
by Marshall (Canon) on Apr 12, 2010 at 17:07 UTC
    to just print a subset of lines, I would capture into a "@" variable and use grep to filter out what you want.
    #!/usr/bin/perl use strict; use warnings; my @result = <DATA>; print grep{m/\.name|\.lastmodifiedtime/}@result; #prints: #task.name:"abc" #task.lastmodifiedtime: 2010-04-12T11:57:56+01:00 by user1@server1 __DATA__ task.start # task.id: 1 task.name:"abc" task.info: xyz task.schedule: task.notify: task.nextruntime: task.lastmodifiedtime: 2010-04-12T11:57:56+01:00 by user1@server1 task.args:
Re: Nested search and print
by kennethk (Abbot) on Apr 12, 2010 at 16:23 UTC
    You can do it simply with regular expressions:

    #!/usr/bin/perl use strict; use warnings; local $/; my $result = <DATA>; print "task name = ", $result =~ /task\.name:(.*)$/m, "\n"; print "task mod time = ", $result =~ /task\.lastmodifiedtime:(.*)$/m, +"\n"; __DATA__ task.start # task.id: 1 task.name:"abc" task.info: xyz task.schedule: task.notify: task.nextruntime: task.lastmodifiedtime: 2010-04-12T11:57:56+01:00 by user1@server1 task.args:

    but this may not be the best choice depending on how much manipulation you want to do. If you are going to want to do more than just print, splitting into a hash or even a hash of hashes may be a better choice.

Re: Nested search and print
by NiJo (Friar) on Apr 12, 2010 at 18:21 UTC
    Unless you have ommitted wider context, I'd not use perl for that:
    #! /bin/sh mt schedule -info | grep task.name mt schedule -info | grep task.lastmodifiedtime