using sed command to delete a string spanning multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed command to delete a string spanning multiple lines
# 1  
Old 07-25-2007
using sed command to delete a string spanning multiple lines

file1 contains the following data

sssssssssss
firstline
secondline pppppppppp
ssssssssss

Using sed comamnd i am trying to delete firtsline secondline.
so, output should be
sssssssssss
pppppppppp
ssssssssss

I tried in the following the way, but it is not working.
sed s/"firstline[\r\t]secondline"// file1
# 2  
Old 07-25-2007
Code:
sed -e '/firstline/d' -e '/secondline/d' input_file

# 3  
Old 07-25-2007
Quote:
Originally Posted by Shell_Life
Code:
sed -e '/firstline/d' -e '/secondline/d' input_file

I Think this would delete the lines containing the pattern secondline. But the OP wanted to remove the pattern from the line.

I think you can use like

Code:
sed 's/.*line//g' file1
or

sed -e 's/firstline//g' -e 's/secondline//g' file1

# 4  
Old 07-25-2007
i forgot to mention, the input file can be

sssssssssss
firstline secondline pppppppppp
ssssssssss
or
sssssssssss
firstline
secondline pppppppppp
ssssssssss

ie firtsline secondline can be on the same line or on different lines, and we should only delete only if we can find both firstline and secondline continueously. otherwise it should be retained
# 5  
Old 07-25-2007
Input

Code:
$ >cat file1
sssssssssss
firstline
secondline pppppppppp
ssssssssss

sssssssssss
firstline secondline pppppppppp
ssssssssss

Output
Code:
$ > sed -e 's/firstline//g' -e 's/secondline//g' file1
sssssssssss

 pppppppppp
ssssssssss

sssssssssss
  pppppppppp
ssssssssss

# 6  
Old 07-25-2007
Quote:
Originally Posted by radha.kalivar
i forgot to mention, the input file can be

sssssssssss
firstline secondline pppppppppp
ssssssssss
or
sssssssssss
firstline
secondline pppppppppp
ssssssssss

ie firtsline secondline can be on the same line or on different lines, and we should only delete only if we can find both firstline and secondline continueously. otherwise it should be retained
By continuously what does that mean,

line after line

Code:
firstline
secondline

or within a same line

Code:
firstline secondline

# 7  
Old 07-25-2007
Acccording to my requirement it should not delete third 'firstline' because there is no 'secondline' string after that. it should delete only if 'firstline secondline' comes contionusly. It can have \n in between.

bash-3.00$cat file1
hello
ssssssssss
firstline
secondline pppppppppp
ssssssssss

sssssssssss
firstline secondline pppppppppp
ssssssssss
firstline ssssssssssss

bash-3.00$ sed -e 's/firstline//g' -e 's/secondline//g' file1
ssssssssss

pppppppppp
ssssssssss

sssssssssss
pppppppppp
ssssssssss
ssssssssssss

bash-3.00$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

delete the line with a particular string in a file using sed command.

Hello, I want to delete all lines with given string in file1 and the string val is dynamic. Can this be done using sed command. Sample: vars="test twinning yellow" for i in $vars do grep $i file1 if then echo "Do Nothing" else sed `/$i/d` file1 fi done Using the above... (5 Replies)
Discussion started by: PrasadAruna
5 Replies

2. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

3. Shell Programming and Scripting

sed to conditionally delete multiple lines

Hello I'd like to remove any line in an output file that is preceded by one or more warning messages (each warning is on a separate line). Example : WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0. 822 28447 ... (4 Replies)
Discussion started by: jossojjos
4 Replies

4. 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

5. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

6. Shell Programming and Scripting

Delete a line between selected lines using sed or any other command

I want to delete a line between selected lines using sed: e.g. : Between "bus" to "pins", delete lines conaining "signal" word. Input : bus direction signal new signal old pins signal ok end Desired Output: bus direction pins signal end (4 Replies)
Discussion started by: nehashine
4 Replies

7. Shell Programming and Scripting

Script to display record spanning over multiple lines

Following are the lines from /etc/sudoers.conf bob SPARC = (OP) ALL : SGI = (OP) ALL fred ALL = (DB) NOPASSWD: ALL ALL CDROM = NOPASSWD: /sbin/umount /CDROM,\ /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM Could you please help me with shell/perl script to display the records with... (2 Replies)
Discussion started by: Ujan
2 Replies

8. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies

9. UNIX for Dummies Questions & Answers

Delete multiple lines containting a variable string using SED.

Good morning, Novice scripter in Unix here, and I've run into and sed task I can't quite wrap my head around. I'm pulling my hair out fast enough as it is and thought I would go to the knowledge bank. I have a sorted file that I'm trying to trim down by deleting any line whose first few... (2 Replies)
Discussion started by: selkirk
2 Replies

10. Shell Programming and Scripting

Delete multiple lines w/ sed

Hi all, I am trying to figure out the syntx to delete multiple lines w/ sed. I know the following syntax will delete lines 1 THROUGH 5 from filex: sed 1,5d filex But I wan to delete lines 1 AND 5 (keeping lines 2,3, and 4). Does anyone know how to do this in a single sed statement? ... (2 Replies)
Discussion started by: bookoo
2 Replies
Login or Register to Ask a Question