in reply to Re^3: Search and delete
in thread Search and delete

Sorry, I am new to this forum . Finding it difficult to understand to post here . Here is my code .
my %unitMap; my $cmd = "hpacucli ctrl slot=0 pd all show";; 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+//; next if $row =~ /^unassigned\n\nOK$/; # I am trying to delete the line that starts with unassigned and 2 lin +es after that print "$row\n";

Replies are listed 'Best First'.
Re^5: Search and delete
by james28909 (Deacon) on Dec 16, 2014 at 01:25 UTC
    I am pretty sure you are trying to delete 3 lines, in which you are only reading one line at a time.

    I could handle this by putting in a counter:
    my $counter = 0; if ( $_ =~ /unassigned/ ) { $_ =~ s/.*\n//gi; #delete unassigned line $counter++; #count will be 1 and will be caught by next if stat +ement } if ( $counter = 1 ) { $_ =~ s/.*\n//gi; #delete next line $counter++; #count will be 2 and will be caught by next if stat +ement } if ( $counter = 2 ) { s/.*\n//gi; #delete the 2nd line after unassigned $counter = 0; #make count to 0 until $_ is something you are lo +oking for }
    I dont know how good this will work for your particular problem, but it could give you another way to think about it :)

    EDIT: I did not read the rest of the comments, seems there was a better answer already given.
Re^5: Search and delete
by Anonymous Monk on Dec 16, 2014 at 00:00 UTC
    Ok, thats code and pattern, but where is the data?
    <code> data data data </code>
      Here is the Data
      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 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

        It seems that you are not really deleting lines from a file, more filtering the output of a command.

        Would it be safe to assume that you would want to resume printing when the next line starts with 'array'? If so, I would suggest setting a print flag to 1 initially, and then in the loop setting it to 0 if 'unassigned' shows up, and reset it to 1 if 'array' shows up. Then print to output if the print flag = 1.

        my %unitMap; my $cmd = "hpacucli ctrl slot=0 pd all show";; open my $fd, "$cmd|" or return \%unitMap; my $printit = 1; while (my $row = <$fd>) { next if $row =~ /^$|Smart/; $printit = 0 if $row =~ /^\s*unassigned$/; $printit = 1 if $row =~ /^\s*array .$/; next unless $printit; # moved processing to here; no need to manipulate lines you're + not going to print anyway $row =~ s/^\s+//; $row =~ s/[,|)|(]//g; print "$row"; } close $fd;
        1 Peter 4:10
        The unassigned can appear any where in the file . Basically, I am trying to delete the line containing unassigned and the 2 lines after unassigned .