in reply to Re: Text filer and assign
in thread Text filer and assign

code snippet is below :

#!/usr/bin/perl -w use strict; system("pvscan>>pv.txt1"); open my $FH,"pv.txt1" or die "Cannot open file: $!"; while(my $line = <$FH>){ my $pvname = $line =~ /\[\d+\.\d+\sGB\s\/\s\d+\.\d+\sGB\]/i; print $pvname; }

i want the assignment to look like :

pvname = [557.62 GB / 20.50 GB free]

Replies are listed 'Best First'.
Re^3: Text filer and assign
by Corion (Patriarch) on Oct 06, 2015 at 08:27 UTC

    Most likely, you want to use "capturing" (see perlre) to keep the stuff that interests you:

    ... if( $line =~ /(\[\d+\.\d+\sGB\s\/\s\d+\.\d+\sGB\])/i ) { my $pvname = $1; print $pvname; };

    Note that your regular expression does not match the result you want, because your result has the string free in it, while your regular expression will not match that. You should add the string at the appropriate place.

    While it makes debugging easier, you don't need a temp file:

    for my $line (`pvscan`) { ... };
Re^3: Text filTer and assign - regex tools
by Discipulus (Canon) on Oct 06, 2015 at 08:36 UTC
    I humly think you need a regex playfield: as usual i suggest two tools:


    I find also YAPE::Regex::Explain useful:
    >perl -MYAPE::Regex::Explain -e " print YAPE::Regex::Explain->new(qr/\ +[\d+\.\d+\sGB\s\/\s\d+\.\d+\sGB\]/i)->explain();" The regular expression: (?i-msx:\[\d+\.\d+\sGB\s/\s\d+\.\d+\sGB\]) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?i-msx: group, but do not capture (case-insensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \. '.' ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- GB 'GB' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- / '/' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \. '.' ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- GB 'GB' ---------------------------------------------------------------------- \] ']' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------


    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^3: Text filer and assign
by johngg (Canon) on Oct 06, 2015 at 13:23 UTC
    system("pvscan>>pv.txt1"); open my $FH,"pv.txt1" or die "Cannot open file: $!";

    I see that you append to the pv.txt1 file which means more lines to parse every time you run the script; is this actually intended? Unless you want to keep the output of pvscan hanging around you can open a file handle directly on piped commands. Something like:-

    open my $pvscanFH, '-|', 'pvscan' or die "Cannot fork pvscan: $!\n";

    You can then read the command output line by line just as if it was a file on disk.

    I hope this is helpful.

    Cheers,

    JohnGG