LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 07-15-2008, 07:26 AM   #1
Mleahy
Member
 
Registered: Jan 2004
Posts: 35
Thanked: 0
Muilti-line text search/replace


[Log in to get rid of this advertisement]
I know this is a common question, but I haven't found a solution that works yet.

Basically, I have a file that has many instances of a block of text, like:

line1
line2
line3


And I want to replace this with a new single line of text:

newline4


It seems that text searches using sed or vi only match single lines of text...so anything that spans multiple lines is no good. E.g.:

sed s/line1\n/line2\n/line3\n/newline4/g infile > outfile

This produces no matches, and the text in the outfile is identical to infile.

I've been searching around for a solution for about an hour, and nothing sensible has turned up.

Does anyone have any ideas?
Mleahy is offline  
Tag This Post , , , ,
Reply With Quote
Old 07-15-2008, 07:45 AM   #2
Mleahy
Member
 
Registered: Jan 2004
Posts: 35
Thanked: 0

Original Poster
Ok...I figured out a reasonable workaround (at least, one that works in my case):

sed s/line1/newline4/g infile | grep -v line2 | grep -v line3 > outfile


This replaces the first line, and filters out line2 & line3 individually. The problem with this is that it's treating each line individually. As long as it doesn't matter whether lines 1 - 3 are in a group, or occur individually, then this is okay (which just happens to be the case for me), but I'd rather replace the text as a block if I can (I know there will be other situations where I'll need to do that).
Mleahy is offline     Reply With Quote
Old 07-15-2008, 08:08 AM   #3
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,173
Thanked: 285
How about using a range and the "c" command?

Let's say you want to find a pattern beginning with a line containing "fred" and ending with a line containing "alice". Then replace this (multi-line) pattern with "susan":

sed '/fred/,/alice/c susan' filename

Good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
pixellany is offline     Reply With Quote
Old 07-15-2008, 08:27 AM   #4
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,134
Thanked: 465
You can add the beginning (^) and termination ($) special characters to the pixellany's solution to make the regular expressions match unique lines:
Code:
sed '/^line1$/,/^line3$/c newline' infile
otherwise /line1/ and /line3/ will match every other line containing "line1" and "line3" (such as "hello line1" or "line3xyz").

For other solutions see the SED FAQ, here.
colucix is offline     Reply With Quote
Old 07-15-2008, 08:37 AM   #5
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,173
Thanked: 285
Quote:
Originally Posted by colucix View Post
You can add the beginning (^) and termination ($) special characters to the pixellany's solution to make the regular expressions match unique lines:
Code:
sed '/^line1$/,/^line3$/c newline' infile
otherwise /line1/ and /line3/ will match every other line containing "line1" and "line3" (such as "hello line1" or "line3xyz").
Indeed....
When using a range (or any other regex), you have to put in **enough** information to get the desired result
pixellany is offline     Reply With Quote
Old 07-15-2008, 09:02 AM   #6
Mleahy
Member
 
Registered: Jan 2004
Posts: 35
Thanked: 0

Original Poster
Very cool...thanks for the tips.
Mleahy is offline     Reply With Quote
Old 07-15-2008, 09:14 AM   #7
Mleahy
Member
 
Registered: Jan 2004
Posts: 35
Thanked: 0

Original Poster
Er...just out of curiosity...is it possible to do the same in vi? I'm trying to get the hang of that as well...I found out how to do the equivalent of s/search/replace/g (hit colon, then enter %s/search/replace/). I'm sure there's something in the vi manual somewhere...
Mleahy is offline     Reply With Quote
Old 07-15-2008, 09:35 AM   #8
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,134
Thanked: 465
In vi use the syntax as in your first attempt with sed:
Code:
:%s/line1\nline2\nline3/newline/g
this works for me (using vim 7.1).
colucix is offline     Reply With Quote
Old 07-15-2008, 09:58 AM   #9
Mleahy
Member
 
Registered: Jan 2004
Posts: 35
Thanked: 0

Original Poster
Awesome, thanks again.
Mleahy is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 08:54 AM
vim search and replace only one line hakmed Programming 5 11-07-2006 08:13 PM
another question about search and replace text graziano1968 Linux - General 3 08-02-2006 10:35 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 07:47 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM


All times are GMT -5. The time now is 06:46 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration