Home
Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

searching multiple lines and replacing in shell scripting

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-19-2011
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 10:19 AM.. Reason: Code tags.
# 2  
Old 05-19-2011
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
The Following User Says Thank You to birei For This Useful Post:
anil8103 (05-20-2011)
# 3  
Old 05-20-2011
Thanks dude
Its working great
# 4  
Old 05-24-2011
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
# 5  
Old 05-24-2011
For a new problem, please make a new post.
The Following User Says Thank You to Chirel For This Useful Post:
anil8103 (05-24-2011)
# 6  
Old 05-24-2011
Ok,, Will make it a new post and thanks for info
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help in replacing a variable in shell scripting jawaugh Shell Programming and Scripting 4 06-20-2014 06:22 PM
Shell script to read lines in a text file and filter user data Shell Programming and Scripting VikrantD UNIX for Dummies Questions & Answers 3 03-25-2014 04:28 AM
Replacing a single line with multiple lines in a file Priya Amaresh Shell Programming and Scripting 4 03-11-2013 05:21 PM
Bash Scripting Help to automate replacing multiple lines Silverlink34 Shell Programming and Scripting 8 12-19-2012 09:18 AM
Replacing System.out.println with Logger.println in *.java using SED (spanned over multiple lines) Bhanu Dhulipudi Shell Programming and Scripting 4 08-13-2012 09:24 AM
How to process multiple input files using Shell scripting evelibertine Shell Programming and Scripting 0 09-27-2011 03:00 PM
Nawk help searching for multiple lines and multiple searches tthach830 Shell Programming and Scripting 3 06-08-2011 04:39 AM
Replacing pattern spanning multiple lines zorrox Shell Programming and Scripting 5 05-02-2011 10:57 AM
replacing a quote in some lines with multiple quote fields wradwan Shell Programming and Scripting 3 06-02-2010 07:53 AM
concatenate lines using shell scripting dtdt Shell Programming and Scripting 13 09-07-2009 01:14 AM
shell scripting-processing multiple scripts RSC1985 Shell Programming and Scripting 2 07-06-2009 01:35 PM
searching thru or combining multiple lines in a unix file ndedhia1 Shell Programming and Scripting 0 03-16-2009 10:17 AM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 03:43 AM
searching through a file and replacing Gerry405 UNIX for Dummies Questions & Answers 2 10-04-2005 09:29 AM
replacing multiple lines ajnabi Shell Programming and Scripting 1 04-09-2002 07:55 PM


All times are GMT -4. The time now is 08:47 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password