Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Regex matching and setting variables

by linebacker (Scribe)
on Aug 09, 2007 at 00:48 UTC ( #631437=perlquestion: print w/replies, xml ) Need Help??

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

I have questions on two items. One is syntax related. I know what I want to do, but don't know the syntax to do it in perl. I want to set the variable $plex and $vol for columns two and three in the following output. Summarily, when column one is equal to /^pl/ and column two contains /-02/ then set the plex variable to column two and capture the contents of column three on a line by line basis. Finally, I want to pass those variables to a Unix command (vxplex -g rootdg att $vol $plex). The second question relates to passing the -T option to perl. When I set perl to use taint checking and I ENV={PATH}, perl complains about my using strict.
#!/usr/bin/perl -w use strict; my @vxvols = `/usr/sbin/vxprint -htqg rootdg`; foreach my $type (@vxvols) { if ( $type =~ /pl/ ) { print ($type) ; } } pl Flars-01 Flars ENABLED ACTIVE 20982912 RAID 3/32 + RW pl Flars-02 Flars ENABLED LOG 10176 CONCAT - + RW pl backup-01 backup ENABLED ACTIVE 33560448 CONCAT - + RW pl backup-02 backup ENABLED ACTIVE 33560448 CONCAT - + RW pl backup-03 backup ENABLED ACTIVE LOGONLY CONCAT - + RW pl export_install-01 export_install ENABLED ACTIVE 209727360 RAID 3/32 + RW pl export_install-02 export_install ENABLED LOG 10176 CONCAT - + RW pl rootdisk7vol-01 rootdisk7vol ENABLED ACTIVE 213696 CONCAT - + RW pl rootdisk7vol-02 rootdisk7vol ENABLED ACTIVE 213696 CONCAT - + RW pl rootvol-01 rootvol ENABLED ACTIVE 63427008 CONCAT - + RW pl rootvol-02 rootvol ENABLED ACTIVE 63427008 CONCAT - + RW pl rootvol-03 rootvol ENABLED ACTIVE LOGONLY CONCAT - + RW pl runbook-01 runbook ENABLED ACTIVE 2116608 RAID 3/32 + RW pl runbook-02 runbook ENABLED LOG 10176 CONCAT - + RW pl swapvol-01 swapvol ENABLED ACTIVE 33550272 CONCAT - + RW pl swapvol-02 swapvol ENABLED ACTIVE 33550272 CONCAT - + RW pl var-01 var ENABLED ACTIVE 12587712 CONCAT - + RW pl var-02 var ENABLED ACTIVE 12587712 CONCAT - + RW pl var-03 var ENABLED ACTIVE LOGONLY CONCAT - + RW
֧

Replies are listed 'Best First'.
Re: Regex matching and setting variables
by GrandFather (Sage) on Aug 09, 2007 at 01:23 UTC

    The following may get you started:

    use warnings; use strict; my @vxvols = <DATA>; for (@vxvols) { chomp; my @cols = split; next unless $cols[0] =~ /^pl\b/; next unless $cols[1] =~ /-02/; print "vxplex -g rootdg att $cols[2] $cols[1]\n"; } __DATA__ pl Flars-01 Flars ENABLED ACTIVE 20982912 RAID 3/32 + RW pl Flars-02 Flars ENABLED LOG 10176 CONCAT - + RW ...

    Prints:

    vxplex -g rootdg att Flars Flars-02 vxplex -g rootdg att backup backup-02 vxplex -g rootdg att export_install export_install-02 vxplex -g rootdg att rootdisk7vol rootdisk7vol-02 vxplex -g rootdg att rootvol rootvol-02 vxplex -g rootdg att runbook runbook-02 vxplex -g rootdg att swapvol swapvol-02 vxplex -g rootdg att var var-02

    DWIM is Perl's answer to Gödel
Re: Regex matching and setting variables
by Errto (Vicar) on Aug 09, 2007 at 01:23 UTC
    I would use split. You can use the special split on whitespace to turn your columns into elements of a list like so
    my @cols = split ' ', $type;
    and then go from there. For your second question, it depends. Are you sure it's strict that's complaining and not the taint checker? What does the code look like for "ENV={PATH}" that you mentioned?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2019-10-22 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?