searching multiple lines and replacing in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching multiple lines and replacing in shell scripting
# 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
This User Gave Thanks to birei For This Post:
# 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.
This User Gave Thanks to Chirel For This Post:
# 6  
Old 05-24-2011
Ok,, Will make it a new post and thanks for info
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Export Oracle multiple tables to multiple csv files using UNIX shell scripting

Hello All, just wanted to export multiple tables from oracle sql using unix shell script to csv file and the below code is exporting only the first table. Can you please suggest why? or any better idea? export FILE="/abc/autom/file/geo_JOB.csv" Export= `sqlplus -s dev01/password@dEV3... (16 Replies)
Discussion started by: Hope
16 Replies

2. Shell Programming and Scripting

Need help in replacing a variable in shell scripting

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... (4 Replies)
Discussion started by: jawaugh
4 Replies

3. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

4. Shell Programming and Scripting

Replacing a single line with multiple lines in a file

Hi Am confused with the usage of "sed" command I want to replace a single line with multiple lines of a file.. eg., A file has Hi, How are you? I need to replace as Am fine What are You doing? I used the script as string1="Hi, How are you?" echo "$string1 is the value"... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

5. Shell Programming and Scripting

Bash Scripting Help to automate replacing multiple lines

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... (8 Replies)
Discussion started by: Silverlink34
8 Replies

6. Shell Programming and Scripting

Nawk help searching for multiple lines and multiple searches

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... (3 Replies)
Discussion started by: tthach830
3 Replies

7. Shell Programming and Scripting

Replacing pattern spanning multiple lines

Hi. I have input like this: <tr> <td class="logo1" rowspan="2"><a href="index.html"><img src="images/logo.png" /></a></td> <td class="pad1" rowspan="2">__</td> <td class="userBox"><img src="images/person.png"/> <a href="http://good.mybook.com/login.jsp">Sign In</a></td> <td... (5 Replies)
Discussion started by: zorrox
5 Replies

8. Shell Programming and Scripting

searching thru or combining multiple lines in a unix file

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... (0 Replies)
Discussion started by: ndedhia1
0 Replies

9. Shell Programming and Scripting

replacing multiple lines with single line

Can any one give me the idea on replacing multiple blank lines with a single blank line? Please conside it for a file having more than 100 number of characters. Regards, Siba (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

10. Shell Programming and Scripting

replacing multiple lines

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" ... (1 Reply)
Discussion started by: ajnabi
1 Replies
Login or Register to Ask a Question