searching multiple lines and replacing in shell scripting


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
searching multiple lines and replacing in shell scripting

Hi,

I have a file with below contents,

Code:
[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

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

Please help me .

Great day
Anil.G

Last edited by radoulov; 05-19-2011 at 09:19 AM.. Reason: Code tags.
# 2  
Hi,

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

Code:
$ 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
This User Gave Thanks to birei For This Post:
# 4  
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
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Need help in replacing a variable in shell scripting
jawaugh
Experts, Can someone help me with the below? I've a variable called NSPMHOME=some_value. I would like change the value of the variable with another variable and something followed by it. E.g From NSPHOME=some_value to NSPMHOME=$SOMETHING/test $SOMETHING has value /opt/tag . The end...... Shell Programming and Scripting
4
Shell Programming and Scripting
Bash Scripting Help to automate replacing multiple lines
Silverlink34
Background: I am writing a script to help me automate tweaks and things I apply to a custom Android rom I developed. I am on the very last part of my script, and I am stuck trying to find the right command to do what I seek. When I build roms from source, a file called updater-script is...... Shell Programming and Scripting
8
Shell Programming and Scripting
Nawk help searching for multiple lines and multiple searches
tthach830
I use this command to find a search (Nr of active alarms are) and print one line before and 10 lines after the search keywords. nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=1 a=10 s="Nr of active alarms are:" *.log However, I would like to know how to tell it to print...... Shell Programming and Scripting
3
Shell Programming and Scripting
searching thru or combining multiple lines in a unix file
ndedhia1
This is the problem actually: This regex: egrep "low debug.*\".*\"" $dbDir/alarmNotification.log is looking for data between the two quotation marks: ".*\" When I hate data like this: low debug 2009/3/9 8:30:20.47 ICSNotificationAlarm Prodics01ics0003 IC...... Shell Programming and Scripting
0
Shell Programming and Scripting
replacing multiple lines
ajnabi
i have a file : sample1.txt OBJECT="POINT" ACTION="REDEFINE" POINT_NAME="ABCD001G " GHYT_POPRIORITY_1="1" GHYT_POPRIORITY_2="1" GHYT_POPRIORITY_3="1" GHYT_POPRIORITY_4="1" GHYT_POPRIORITY_USER="1" HIGH_ALARM_PRIORITY_1="1" HIGH_ALARM_PRIORITY_2="1" HIGH_ALARM_PRIORITY_3="1" ...... Shell Programming and Scripting
1
Shell Programming and Scripting