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

I only want whats under MOUNT POINT or /tmp/sapinst_instdir and /usr/sap. Any help ? thx
# perl -le 'system("lspv -l vpath0"); print +(split)[4], $_;' vpath0: LV NAME LPs PPs DISTRIBUTION MOUNT POINT loglv51 1 1 01..00..00..00..00 N/A lvsapinst 64 64 54..10..00..00..00 /tmp/sapinst_i +nstdir lvusrsap 25 25 00..25..00..00..00 /usr/sap

Replies are listed 'Best First'.
Re: split question
by FunkyMonk (Bishop) on Nov 12, 2007 at 20:10 UTC
    system will display the command output on the console. You want backticks (documented under Quote and Quote like Operators in perlop) that will capture the output to a variable:

    for ( `lspv -l vpath0` ) { print +(split)[4], "\n"; }

    but that's going to include the first two lines of the output, which you don't(?) want. So pipe the output of the command to perl:

    open my $output, "lspv -l vpath0 |" or die $!; <$output>; # discard first <$output>; # two lines for ( <$output> ) { print +(split)[4], "\n"; }

      Cool. Thank you! But now I have decided to print fields 1 and 4, however I cannot get past this error:
      Use of uninitialized value in concatenation (.) or string at san_relat +ion.plx line 81, <$output> line 3.

      # @vpaths contains vapt0..vpath## for my $vps (@vpaths) { print "\n"; open my $output, "lspv -l $vps |" or die $!; #<$output>; #<$output>; for (<$output>) { next if m[distribution|n/a]i; next unless defined; print Dumper(my ($f1,$f4) = (split)[0, 4]); print "$f1\t$f4\n"; #print +(split)[0],"\n"; #print +(split)[4],"\n"; } print "\n"; #system("odmget -q name=$vps 'CuAt'|grep -p pvid"); } from the Dumper output VAR2 is the problem $VAR1 = 'vpath0:';
      $VAR2 = undef;
      Use of uninitialized value in concatenation (.) or string at san_relat +ion.plx line 81, <$output> line 3. vpath0: $VAR1 = 'lvREPdata10'; $VAR2 = '/db2/REP/sapdata10'; lvREPdata10 /db2/REP/sapdata10
Re: split question
by GrandFather (Saint) on Nov 12, 2007 at 20:24 UTC

    If you are extracting data from fixed width columns as seems to be the case here then you can use any of a number of techniques:

    use strict; use warnings; my $table = <<DATA; LV NAME LPs PPs DISTRIBUTION MOUNT POINT loglv51 1 1 01..00..00..00..00 N/A lvsapinst 64 64 54..10..00..00..00 /tmp/sapinst_i +nstdir lvusrsap 25 25 00..25..00..00..00 /usr/sap DATA open IN, '<', \$table; while (<IN>) { chomp; my $name = substr $_, 0, 22; my $dist = substr $_, 34, 18; my $mount = substr $_, 56; print "$name $dist $mount\n"; } close IN; open IN, '<', \$table; while (<IN>) { chomp; my ($name, $dist, $mount) = /(.{22}).{12}(.{18}).{4}(.*)/; print "$name $dist $mount\n"; } close IN; open IN, '<', \$table; while (<IN>) { chomp; my ($name, undef, $dist, undef, $mount) = unpack ('a22a12a18a4a*', + $_); print "$name $dist $mount\n"; } close IN;

    Prints:

    LV NAME DISTRIBUTION MOUNT POINT loglv51 01..00..00..00..00 N/A lvsapinst 54..10..00..00..00 /tmp/sapinst_instdir lvusrsap 00..25..00..00..00 /usr/sap LV NAME DISTRIBUTION MOUNT POINT loglv51 01..00..00..00..00 N/A lvsapinst 54..10..00..00..00 /tmp/sapinst_instdir lvusrsap 00..25..00..00..00 /usr/sap LV NAME DISTRIBUTION MOUNT POINT loglv51 01..00..00..00..00 N/A lvsapinst 54..10..00..00..00 /tmp/sapinst_instdir lvusrsap 00..25..00..00..00 /usr/sap

    Interestingly, none of these use split.


    Perl is environmentally friendly - it saves trees