LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > 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



Reply
 
Search this Thread
Old 07-15-2008, 07:26 AM   #1
Mleahy
Member
 
Registered: Jan 2004
Posts: 43

Rep: Reputation: 15
Muilti-line text search/replace


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?
 
Old 07-15-2008, 07:45 AM   #2
Mleahy
Member
 
Registered: Jan 2004
Posts: 43

Original Poster
Rep: Reputation: 15
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).
 
Old 07-15-2008, 08:08 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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
 
Old 07-15-2008, 08:27 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 07-15-2008, 08:37 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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
 
Old 07-15-2008, 09:02 AM   #6
Mleahy
Member
 
Registered: Jan 2004
Posts: 43

Original Poster
Rep: Reputation: 15
Very cool...thanks for the tips.
 
Old 07-15-2008, 09:14 AM   #7
Mleahy
Member
 
Registered: Jan 2004
Posts: 43

Original Poster
Rep: Reputation: 15
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...
 
Old 07-15-2008, 09:35 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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).
 
Old 07-15-2008, 09:58 AM   #9
Mleahy
Member
 
Registered: Jan 2004
Posts: 43

Original Poster
Rep: Reputation: 15
Awesome, thanks again.
 
  


Reply

Tags
multiline, replace, search, sed, text


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:18 AM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration