Re: remove 16 Zeros
by flexvault (Monsignor) on Oct 13, 2015 at 09:23 UTC
|
my $pvid = "00c1be9a467335ce0000000000000000";
my $newpvid = substr( $pvid, 0, 16 );
print "\$newpvid: $newpvid\n";
but also reading some more documentation wouldn't hurt!
Regards...Ed
"Well done is better than well said." - Benjamin Franklin
| [reply] [d/l] |
|
Hi! Yes thank u for Response. PVID is allways 32
Charachters.
My Script is:
@output =`command` # Output looks like 7 lines and on line is the pvid:
pvid 00c1be9a467335ce0000000000000000
foreach $line (@output) {
if ($line =~ /pvid ..... here i want to grep only the 32 charachters
$pvid = $1
}
i just want to get the $pvid.
| [reply] |
|
############# UNTESTED ################
use strict;
my $pvid = ''; # initialize
my @output = `command`; # Output looks like 7 lines and on [one] li
+ne is the pvid:
# pvid 00c1be9a467335ce0000000000000000
# I use qx/command/; # easier to see in cod
+e for me!
foreach my $line ( @output )
{ # print "$line\n"; ## If not working, use print to figure out w
+hat's happening
if ( substr( $line, 0, 5 ) eq 'pvid ' )
{ $pvid = substr( $line, 6, 16 ) # or '32' as you require
last;
}
}
if ( $pvid eq '' ) { ... } ## Generate warning or do some error h
+andling!
I'm coding to my style, but you are free to chose what works for you. But remember if you have to go back to the code and update, make it as easy to understand as possible. Don't spend 2 hours figuring out what you did and then make a 2 line update.
Good Luck...Ed
"Well done is better than well said." - Benjamin Franklin
| [reply] [d/l] |
Re: remove 16 Zeros
by mr_ron (Chaplain) on Oct 13, 2015 at 14:34 UTC
|
use strict;
use warnings;
my $line = "pvid 00c1be9a467335ce0000000000000000";
my $pvid;
if ($line =~ /pvid\s+(.*)0{16}$/) {
$pvid = $1
}
print "$pvid\n";
__END__
00c1be9a467335ce
Since the string you are trying to extract consists of hex digits, a slight improvement on the regex might be: /pvid\s+([[:xdigit:]]*)0{16}$/ . Go to perldoc.perl.org and search for "character class" to learn more ...
| [reply] [d/l] [select] |
|
Oh yeah. that works fine. I have to find out about 1000 PVID's from DISKS. So i list DISKS and read the Configuration of DISK's, where i must grep the string pvid and
filter only he pvid.
So this is the better solution.
thank u Ron :-)
| [reply] |
Re: remove 16 Zeros
by ww (Archbishop) on Oct 13, 2015 at 12:20 UTC
|
Frankly, I find your question hard to understand... and not just because you failed to use proper markup -- i.e., code tags (<c> ... </c>) around data and code. Please see the markup instructions at the text entry box or read Writeup Formatting Tips).
But addressing what I surmise you may think is a partial approach to your problem (including the possibility of a variable char count before your 16 zeros), the following should do the job:
#! /usr/bin/perl -w
use strict;
# 1144651
my $pvid;
my $line = "pvid 00c1be9a467335ce0000000000000000";
if ($line =~ /(pvid .*?)0000000000000000$/ ) {
$pvid = $1;
}
print $pvid;
Your "partial approach" " if ($line =~ /pvid\+(.*$)s{0000000000000000}{ $pvid = $1 }"
- provides no mechanism to capture the initial four chars, "pvid", nor any limitation on how many times to accept what the dot represents (a single instance of anything, char, num, symbol....).
- In a regex, the "$" marks the end of an input line, so an input with additional data -- if you had any limit on the .* (anything, any number of times) -- would never match.
- Then -- as written -- your curly braces enclosing the zeros are a quantifier for how many times to match "s"
- Finally, you don't terminate the "partial approach's" regex with a closing slash, "/".
As a prophet once said, you can't just make stuff up and expect the computer to understand.
Note also the "my $pvid which makes the variable global, and allows perl to pass its content back to the code outside the if clause.
As others observed, best you read docs such as perldoc perlre and perldoc perlretut... available at your own console... and perhaps tutorials (Tutorials) here such as Getting Started with Perl.
Updated: textual typos corrected and formatting changed for readability.
| [reply] [d/l] [select] |
|
thank u very much. I am just in Process to get to manual.
again thank u.
| [reply] |
|
Your installation of Perl includes a "manual". Use the perldoc command to access it.
Also, all of the "official" Perl documentation is available online at http://perldoc.perl.org
| [reply] [d/l] |
Re: remove 16 Zeros
by Anonymous Monk on Oct 13, 2015 at 08:55 UTC
|
s{0000000000000000}{}; ## remove 16 zeros
| [reply] [d/l] |
|
| [reply] |
|
Folk, i need help again :-(
how do i save this value (pvid) to a
sub function in perl?
there are keys defined in the sub function and they are pushed and later prints in html:
push(@{$lsmap_slots{$key}}, $svsa);
push(@{$lsmap_slots{$key}}, $physloc);
push(@{$lsmap_slots{$key}}, $vtd);
push(@{$lsmap_slots{$key}}, $lun);
push(@{$lsmap_slots{$key}}, $backing);
push(@{$lsmap_slots{$key}}, $bdphysloc);
push(@{$lsmap_slots{$key}}, $status);
how do i push pvid in this sub function?>
i know, i have to read but Perl is very heavy for me at the Moment.
Kind Regards | [reply] [d/l] |
|
|