We need to match certain data element by element that is an output in tabular form obtained on the command prompt.The following is the approach being currently followed wherein the $Var contains the output. Is there an optimal way of doing this without directing the command output to file.
Please share your thoughts.
$Var = "iSCSI Storage LHN StgMgmt Name IP Name
==============================================================
0 Storage_1 15.178.209.194 admin
1 acct-mgmt 15.178.209.194 storage1
2 acct-mgmt2 15.178.209.194 storage2";
@tab = split("\n",$Var);
foreach (@tab) {
next if ($_ !~ /^\d/);
$_ =~ s/\s+//g;
$first=0 if($_ =~ /Storage/i && /15.178.209.194/);
push(@Array, $_); }
$_ =~ /Storage/i && /15.178.209.194/is silly. That gets broken up like this:($_ =~ /Storage/i) && (/15.178.209.194/). Either use$_consistently or don’t – the//ands///operators automatically operate on$_.Also you should know that in the regex
/15.178.209.194/, the.s are being interpreted as any character. Either escape them or use theindex()function.Additionally, I would recommend that you separate each line using
split(). This allows you to compare each individual column. You can usesplit()with a regex like so:@array = split(/\s+/, $string);.Finally, I’m not really sure what
$firstis for, but I notice that all three sample lines in that input trigger$first=0as they all contain that IP and the string “storage”.