searching multiple lines and replacing in shell scripting

Hi,

I have a file with below contents,

[abc:/def/ghi/jkl/mno]
ssenthil = rw
[abc:/def/ghi/jkl/mno]
anilkg = rw

I want to search for "ssenthil" and need to delete line 1 and 2 , if the third line starts with "[" or blank line.

I need to perform with following steps
1 - searching "ssenthil" and deleting the line.

2 -Now i want to delete by the option that the line starts and end with "[" and "]" respectively and blank line immediately and third line starts with "[" should delete the first two lines.

to get the output as below

[abc:/def/ghi/jkl/mno]
anilkg = rw

Please help me .

Great day
Anil.G

Hi,

I'm not sure I understood your problem correctly. Test next 'Perl' script. Perhaps it works:

$ cat script.pl
use strict;
use warnings;
use autodie;

@ARGV == 1 or die "Usage: perl $0 infile\n";
open my $fh, "<", $ARGV[0];
my ($flag, $line);

while ( <$fh> ) {
    if ( /^(?i:ssenthil)/ ) {
        $flag = 1;
        next;
    } else {
        print $line if defined $line && ! $flag;
    }  
    if ( $flag ) {
        $flag = 0;
        unless ( /^\[.*\]$/ || /^\s*$/ ) {
            print $line;
        }  
    }  
    $line = $_;
}

print $line;
$ perl script.pl infile
(Output supressed)

Regards,
Birei

1 Like

Thanks dude
Its working great

Hi Dude,
I am struck up with another problem,Pls help me
I am new to perl , I have little knowledge only.

The scenario is
[abc:/def/ghi/jkl/mno]
ssenthil = rw
[abc:/def/ghi/jkl/mno]
anilkg = rw
[abc:/def/ghi/jkl/mno]
@group1 = rw
anilkg = rw

your reply works perfectly well, but it deletes @group1, if the line matches "anilkg"

i need output as below

[abc:/def/ghi/jkl/mno]
ssenthil = rw
[abc:/def/ghi/jkl/mno]
@group1 = rw

will you pls suggest me?

Rgds
Anil.G

For a new problem, please make a new post.

1 Like

Ok,, Will make it a new post and thanks for info