Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^6: Join multiple lines in a string based on regex.

by pr33 (Scribe)
on Dec 17, 2014 at 21:17 UTC ( [id://1110678]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Join multiple lines in a string based on regex.
in thread Join multiple lines in a string based on regex.

Sorry, The semicolon is there , But it didn't paste properly . I meant to show that the line in between double quotes is a command and I have opened the O/p of the command as a filehandle. Please ignore the unitMap hash , I will be using it sometime later in the script after parsing the data with the O/p I want .
my $cmd = "hpacucli ctrl slot=0 pd all show";; open my $fd, "$cmd|" or return \%unitMap;
Command = hpacucli ctrl slot=0 pd all show

Replies are listed 'Best First'.
Re^7: Join multiple lines in a string based on regex.
by NetWallah (Canon) on Dec 18, 2014 at 06:26 UTC
    The code I posted properly parses the data you posted, and creates the output you defined.

    ... or return \%unitMap;
    Your code is attempting to "return" if the open fails - where do you expect this to return to ? (You are not in a SUB at that point)

    Also, the hash it is attempting to return is not declared.

    By posting incomplete, and incorrect snippets, you are losing credibility. Without error or diagnostic messages, it is getting impossible to assist you.

    • Show Actual code (Or, preferably, a relavent functional subset that demomstrates the issue)
    • Show actual data to be used (relevant subset) (include spacing .. i.e. are there leading spaces?)
    • Show actual output (relevant subset)
    • Show how the command was run, and error messages, if any
    • Show what you expected it to produce

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

      -> Actual Code :
      use strict; use warnings; my %unitMap; my $skipNext; my $cmd = "hpacucli ctrl slot=0 pd all show"; open my $fd, "$cmd|"; while (my $row = <$fd>) { $row =~ /^$/ and next; $row =~ s/[,|)|(]//g; chomp $row; next if $row =~ /^Smart/g; $row =~ s/^\s+//; if( $row =~ /^unassigned/ ){ $skipNext = 2; next; print "$row\n"; } close($fd);
      Actual Data : This includes leading white spaces . I have exxactly pasted the Command O/P here.
      data Smart Array P410i in Slot 0 (Embedded) array A physicaldrive 2C:1:1 (port 2C:box 1:bay 1, SAS, 1 TB, OK) physicaldrive 2C:1:2 (port 2C:box 1:bay 2, SAS, 1 TB, OK) array B physicaldrive 2C:1:3 (port 2C:box 1:bay 3, SAS, 1 TB, OK) physicaldrive 2C:1:4 (port 2C:box 1:bay 4, SAS, 1 TB, OK) array C physicaldrive 3C:1:5 (port 3C:box 1:bay 5, SAS, 1 TB, OK) physicaldrive 3C:1:6 (port 3C:box 1:bay 6, SAS, 1 TB, OK) array D physicaldrive 3C:1:7 (port 3C:box 1:bay 7, SAS, 1 TB, OK) physicaldrive 3C:1:8 (port 3C:box 1:bay 8, SAS, 1 TB, OK) array E physicaldrive 4C:2:1 (port 4C:box 2:bay 1, SAS, 1 TB, OK) physicaldrive 4C:2:2 (port 4C:box 2:bay 2, SAS, 1 TB, OK) array F physicaldrive 4C:2:3 (port 4C:box 2:bay 3, SAS, 1 TB, OK) physicaldrive 4C:2:4 (port 4C:box 2:bay 4, SAS, 1 TB, OK) array G physicaldrive 5C:2:5 (port 5C:box 2:bay 5, SAS, 1 TB, OK) physicaldrive 5C:2:6 (port 5C:box 2:bay 6, SAS, 1 TB, OK) array H physicaldrive 5C:2:7 (port 5C:box 2:bay 7, SAS, 1 TB, OK) physicaldrive 5C:2:8 (port 5C:box 2:bay 8, SAS, 1 TB, OK) array I physicaldrive 6C:3:1 (port 6C:box 3:bay 1, SAS, 1 TB, OK) physicaldrive 6C:3:2 (port 6C:box 3:bay 2, SAS, 1 TB, OK) array J physicaldrive 6C:3:3 (port 6C:box 3:bay 3, SAS, 1 TB, OK) physicaldrive 6C:3:4 (port 6C:box 3:bay 4, SAS, 1 TB, OK) array K physicaldrive 7C:3:5 (port 7C:box 3:bay 5, SAS, 1 TB, OK) physicaldrive 7C:3:6 (port 7C:box 3:bay 6, SAS, 1 TB, OK) unassigned physicaldrive 7C:3:7 (port 7C:box 3:bay 7, SAS, 1 TB, OK) physicaldrive 7C:3:8 (port 7C:box 3:bay 8, SAS, 1 TB, OK)
      --> Actual O/P
      data array A physicaldrive 2C:1:1 port 2C:box 1:bay 1 SAS 1 TB OK physicaldrive 2C:1:2 port 2C:box 1:bay 2 SAS 1 TB OK array B physicaldrive 2C:1:3 port 2C:box 1:bay 3 SAS 1 TB OK physicaldrive 2C:1:4 port 2C:box 1:bay 4 SAS 1 TB OK array C physicaldrive 3C:1:5 port 3C:box 1:bay 5 SAS 1 TB OK physicaldrive 3C:1:6 port 3C:box 1:bay 6 SAS 1 TB OK array D physicaldrive 3C:1:7 port 3C:box 1:bay 7 SAS 1 TB OK physicaldrive 3C:1:8 port 3C:box 1:bay 8 SAS 1 TB OK array E physicaldrive 4C:2:1 port 4C:box 2:bay 1 SAS 1 TB OK physicaldrive 4C:2:2 port 4C:box 2:bay 2 SAS 1 TB OK array F physicaldrive 4C:2:3 port 4C:box 2:bay 3 SAS 1 TB OK physicaldrive 4C:2:4 port 4C:box 2:bay 4 SAS 1 TB OK array G physicaldrive 5C:2:5 port 5C:box 2:bay 5 SAS 1 TB OK physicaldrive 5C:2:6 port 5C:box 2:bay 6 SAS 1 TB OK array H physicaldrive 5C:2:7 port 5C:box 2:bay 7 SAS 1 TB OK physicaldrive 5C:2:8 port 5C:box 2:bay 8 SAS 1 TB OK array I physicaldrive 6C:3:1 port 6C:box 3:bay 1 SAS 1 TB OK physicaldrive 6C:3:2 port 6C:box 3:bay 2 SAS 1 TB OK array J physicaldrive 6C:3:3 port 6C:box 3:bay 3 SAS 1 TB OK physicaldrive 6C:3:4 port 6C:box 3:bay 4 SAS 1 TB OK array K physicaldrive 7C:3:5 port 7C:box 3:bay 5 SAS 1 TB OK physicaldrive 7C:3:6 port 7C:box 3:bay 6 SAS 1 TB OK
      --> The same command hpacucli ctrl slot=0 pd all show was ran on the command line and I have assigned the command to $cmd variable in the script . No error messages . --> O/P expected to produce .
      data array A physicaldrive 2C:1:1 port 2C:box 1:bay 1 SAS 1 TB OK physicald +rive 2C:1:2 port 2C:box 1:bay 2 SAS 1 TB OK array B physicaldrive 2C:1:3 port 2C:box 1:bay 3 SAS 1 TB OK physicald +rive 2C:1:4 port 2C:box 1:bay 4 SAS 1 TB OK array C physicaldrive 3C:1:5 port 3C:box 1:bay 5 SAS 1 TB OK physicald +rive 3C:1:6 port 3C:box 1:bay 6 SAS 1 TB OK array D physicaldrive 3C:1:7 port 3C:box 1:bay 7 SAS 1 TB OK physicald +rive 3C:1:8 port 3C:box 1:bay 8 SAS 1 TB OK array E physicaldrive 4C:2:1 port 4C:box 2:bay 1 SAS 1 TB OK physicald +rive 4C:2:2 port 4C:box 2:bay 2 SAS 1 TB OK array F physicaldrive 4C:2:3 port 4C:box 2:bay 3 SAS 1 TB OK physicald +rive 4C:2:4 port 4C:box 2:bay 4 SAS 1 TB OK array G physicaldrive 5C:2:5 port 5C:box 2:bay 5 SAS 1 TB OK physicald +rive 5C:2:6 port 5C:box 2:bay 6 SAS 1 TB OK array H physicaldrive 5C:2:7 port 5C:box 2:bay 7 SAS 1 TB OK physicald +rive 5C:2:8 port 5C:box 2:bay 8 SAS 1 TB OK array I physicaldrive 6C:3:1 port 6C:box 3:bay 1 SAS 1 TB OK physicald +rive 6C:3:2 port 6C:box 3:bay 2 SAS 1 TB OK array J physicaldrive 6C:3:3 port 6C:box 3:bay 3 SAS 1 TB OK physicald +rive 6C:3:4 port 6C:box 3:bay 4 SAS 1 TB OK array K physicaldrive 7C:3:5 port 7C:box 3:bay 5 SAS 1 TB OK physicald +rive 7C:3:6 port 7C:box 3:bay 6 SAS 1 TB OK
        Fixed by deleting leading white space, in my code:
        use strict; use warnings; my $row =""; my $inarray=0; while (<DATA>) { chomp; s/^\s+//; # <-- New line added <---- if (/^array|^unassigned/){ print "$row\n" if $inarray; $row=$_; $inarray=m/^array/; next; } next unless $inarray; $row .= " $_"; } print "$row\n" if $inarray; __DATA__

                "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1110678]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found