Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Perl Expect : Get the complete matched string

by cryptonite1 (Initiate)
on Aug 02, 2016 at 05:35 UTC ( [id://1168984]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, how do i get the complete string which has matched via regex when using Perl Expect.

#!/usr/bin/perl use Expect; use Getopt::Long; my $exp = Expect->spawn("/usr/bin/ssh $username\@$ip") or die "Cannot spawn ssh: $!\n";; /* removed the ssh handling code */ $exp->send("show backup status\n"); $result=$exp->expect( $timeout, ['-re',qr'Backup file location:',sub {my $exp = shift; $exp->print_log_file($backup_Complete); $i=0; } ], ['-re',qr'>'] );

Out put of the above command is :

=== local backup succeeded. Backup file location: /var/log/CPbackup/backups/x.x/x.x.x.x_01_1_Aug_2 +016_11_05.tgz Backup process finished in 00:48 seconds ===
How do we extract "location: /var/log/CPbackup/backups/x.x/x.x.x.x_01_1_Aug_2016_11_05.tgz" line from the output with perl expect ? Do note that i have tried to use $exp->match() but just displayes "Backup file location:" which is the regex

Replies are listed 'Best First'.
Re: Perl Expect : Get the complete matched string (updated)
by haukex (Archbishop) on Aug 02, 2016 at 06:37 UTC

    Hi cryptonite1,

    I haven't used Expect much, but the documentation says

    If called in an array context expect() will return ($matched_pattern_position, $error, $successfully_matching_string, $before_match, and $after_match).

    So that would mean something like

    my ($match_pos, $error, $match_string, $before_match, $after_match) = +$exp->expect( $timeout, ... );

    (Untested) Update: Fixed a copy/paste mistake, and I tried it out and in your case you'll probably be interested in the string $after_match, since that will contain the rest of the line after the regex "Backup file location:". It'll still have whitespace and newlines on the ends of the string, so you'll have to do something like $after_match =~ s/^\s+|\s+$//g; to get rid of it.

    Hope this helps,
    -- Hauke D

      Hi Hauke, thanks for the updates. Yes , i did try $after_match but the problem is it will pull all the output after the match is found , all i need is to extact only the specific line "Backup file location: /var/log/CPbackup/backups/x.x/x.x.x.x_01_1_Aug_2016_11_05.tgz"

        Hi cryptonite1,

        What does the string you want to get the filename from look like exactly? Use this to print it:

        use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper($after_match);

        Regards,
        -- Hauke D

Re: Perl Expect : Get the complete matched string
by Anonymous Monk on Aug 02, 2016 at 13:51 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1168984 use strict; use warnings; use Expect; my $exp = Expect->spawn('echo -e "local backup succeeded.\n\nBackup +file location: /var/log/CPbackup/backups/x.x/x.x.x.x_01_1_Aug_2016_11 +_05.tgz\n\nBackup process finished in 00:48 seconds"') or die; my ($pos, $error, $matched, $before, $after) =$exp->expect( 10, ['-re',qr/Backup file location:[^\r\n]*/,sub {my $exp = shift; + } ], ['-re',qr'>'] ); print "\n\n\nfound: <<<$matched>>>\n";;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-20 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found