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

Hi, Thanks for sending the code . I have executed the script , But it doesn't give any O/P . The File Handle that I am opening comes from a Command O/P . Here is what I tried .
my $row =""; my $inarray=0; my $cmd = "Command"; open $fd, "$cmd|" while (<$fd>) { chomp; if (/^array|^unassigned/){ print "$row\n" if $inarray; $row=$_; $inarray=m/^array/; next; } next unless $inarray; $row .= " $_"; } print "$row\n" if $inarray; close($fd);
The actual Data from the command O/P
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)
Output Expected :
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 .......................................
The Code that I has written has parsed the O/P as follows .
#!/usr/bin/perl ############# use strict; use warnings; use Data::Dumper; my %unitMap; my $skipNext; my $cmd = "command";; open my $fd, "$cmd|" or return \%unitMap; 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; } if( $skipNext ){ $skipNext--; next; } print "$row\n";
O/P from my Code :
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

Replies are listed 'Best First'.
Re^5: Join multiple lines in a string based on regex.
by NetWallah (Canon) on Dec 17, 2014 at 20:39 UTC
    Your code is missing a semicolon at the end of this line:
    open $fd, "$cmd|"
    Also - I dont see how running the command "Command" would produce the output you show.

    I would also recommend using "my $fd", otherwise, "strict" would complain.

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

      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
        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