Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

issue with grep in the output of a command

by janasec (Sexton)
on Jul 25, 2015 at 12:12 UTC ( [id://1136282]=perlquestion: print w/replies, xml ) Need Help??

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

issue with grep in the output of a command

I have a script that executes on a remote machine and fetched the output

The output saves in to a variable,I need to grep for a value so i can use it,the sample code is below

$logger->info("performing ssh to the swicth"); my $ssh = Net::OpenSSH->new($ip,user=>"$username",password=>"$password +",master_opts=>[-o =>"StrictHostKeyChecking=no"]); print"$display_message\n"; $logger->info("listing the current defined configuration of the switch + using cfgshow"); my($out,$err) = $ssh->capture2("cfgshow");
the $out contains the output of the cfgshow command the output in $out is
2015/07/25 23:01:48 Test1:pass cfgshow Defined configuration: cfg: FC_DVT MEZZI; MEZZI1 zone: MEZZI mezzi1; mezzi2; sanblaze zone: MEZZI1 mezzi3; mezzi4; sanblaze zone: vikash_enfield 10:00:00:00:c9:a1:95:aa; 10:00:00:00:c9:a1:95:ab; 20:00:00:11:0d:34:56:00 alias: mezzi1 10:00:00:00:c9:cd:cc:6c alias: mezzi2 10:00:00:00:c9:cd:cc:6d alias: mezzi3 10:00:00:90:fa:2e:dd:9f alias: mezzi4 10:00:00:90:fa:2e:dd:a0 alias: sanblaze 20:01:00:11:0d:34:57:00 Effective configuration: cfg: FC_DVT zone: MEZZI 10:00:00:00:c9:cd:cc:6c 10:00:00:00:c9:cd:cc:6d 20:01:00:11:0d:34:57:00 zone: MEZZI1 10:00:00:90:fa:2e:dd:9f 10:00:00:90:fa:2e:dd:a0 20:01:00:11:0d:34:57:00
i need to grep for FC_DVT into anther variable,please help on how to do it I need a variable to contain only "FC_DVT" i dont need other values

Replies are listed 'Best First'.
Re: issue with grep in the output of a command
by pme (Monsignor) on Jul 25, 2015 at 12:49 UTC
    Hi janasec,

    You can split and grep like this:

    use Data::Dumper; my @out = grep /FC_DVT/, split("\n", $out); print Dumper( \@out ) . "\n";
    and the output is
    $VAR1 = [ 'cfg: FC_DVT MEZZI; MEZZI1', 'cfg: FC_DVT' ];
    UPDATE 'use' line was edded.
Re: issue with grep in the output of a command
by CountZero (Bishop) on Jul 25, 2015 at 13:02 UTC
    do {say if /FC_DVT/} for split "\n", $out;

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: issue with grep in the output of a command
by Laurent_R (Canon) on Jul 25, 2015 at 14:23 UTC
    Hi janasec,

    As a Perl one-liner:

    $ echo '2015/07/25 23:01:48 Test1:pass cfgshow Defined configuration: > cfg: FC_DVT MEZZI; MEZZI1 > zone: MEZZI mezzi1; mezzi2; sanblaze > zone: MEZZI1 mezzi3; mezzi4; sanblaze > zone: vikash_enfield > 10:00:00:00:c9:a1:95:aa; 10:00:00:00:c9:a1:95:ab; > 20:00:00:11:0d:34:56:00 > alias: mezzi1 10:00:00:00:c9:cd:cc:6c > alias: mezzi2 10:00:00:00:c9:cd:cc:6d > alias: mezzi3 10:00:00:90:fa:2e:dd:9f > alias: mezzi4 10:00:00:90:fa:2e:dd:a0 > alias: sanblaze > 20:01:00:11:0d:34:57:00 > > Effective configuration: > cfg: FC_DVT > zone: MEZZI 10:00:00:00:c9:cd:cc:6c > 10:00:00:00:c9:cd:cc:6d > 20:01:00:11:0d:34:57:00 > zone: MEZZI1 10:00:00:90:fa:2e:dd:9f > 10:00:00:90:fa:2e:dd:a0 > 20:01:00:11:0d:34:57:00 > ' | perl -e 'print grep /FC_DVT/, <>;' cfg: FC_DVT MEZZI; MEZZI1 cfg: FC_DVT
Re: issue with grep in the output of a command
by ww (Archbishop) on Jul 27, 2015 at 12:26 UTC

    You have code in replies above but for future reference:

    1. We like to see your code (working code, that is, where code which compiles but produces errors or warnings upon execution minimally satisfies the "working" condition)... as a alternative to mere referals to the documents, in this case:
    2. perldoc -f grep

    $anecdote ne $data


    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-26 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found