LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-26-2012, 04:54 AM   #1
haveanother
LQ Newbie
 
Registered: Jan 2011
Posts: 13

Rep: Reputation: 0
Lightbulb How to find and replace a text spanning multiple lines with sed


Hi!

I'm trying to replace a text pattern in a file that spans across multiple lines, using sed. I want sed to be able to match the newline character specifically (and also to match multiple newline characters, for example "\n\n").

I think there is a command line switch for it, but I cant find it in the man page or in sed --help.

How can I make sed match multiple lines?
 
Old 02-26-2012, 04:57 AM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
To ease up on the typing, have a look here - http://stackoverflow.com/questions/1...ce-a-newline-n

Cheers,

Josh
 
Old 02-26-2012, 05:43 AM   #3
haveanother
LQ Newbie
 
Registered: Jan 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for that but it didn't help.

I'm trying to achieve this effect:

Code:
sed -e 's/\n+/\n/g' file.txt
The above doesn't work.

Basically, I have a lot of newlines, and want to collapse them to 1 newline.

This code from the linked page didn't work:

Code:
sed -e ':a;N;$!ba;s/\n/ /g' file.txt
(it eliminated all newlines)

edit: changed the code to the original from the linked page (none of them has worked anyway...)

Last edited by haveanother; 02-26-2012 at 05:48 AM.
 
Old 02-26-2012, 05:58 AM   #4
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Thanks to the internet with this one (was tricky at first...) I figured it out for you man:
Code:
sed '$!N; /^\(.*\)\n\1$/!P; D' file.txt
Cheers,

Josh
 
Old 02-26-2012, 06:02 AM   #5
haveanother
LQ Newbie
 
Registered: Jan 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Here is a solution that worked for me, although it uses perl instead of sed:

Code:
perl -e 'undef $/; $myfile = <STDIN>; $myfile =~ s/\n+/\n/g; print $myfile' < file.txt
Thanks again for the help!
 
Old 02-26-2012, 06:07 AM   #6
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Oh ok, no problem! Does my sed example work for you as well? And since this is solved, can you please mark your thread as solved using the thread tools located at the top of the page? Thanks!

Cheers,

Josh
 
Old 02-26-2012, 06:11 AM   #7
haveanother
LQ Newbie
 
Registered: Jan 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for that, but unfortunately it still produces output that contains 2 consecutive newlines . . .

Using the example file (please see the attached "file.txt")

Code:
sed '$!N; /^\(.*\)\n\1$/!P; D' file.txt
Produces this output:

Code:
asdf

asdf

asdf

asdf

asdf

vbmn

xcv

While this one:

Code:
perl -e 'undef $/; $myfile = <STDIN>; $myfile =~ s/\n+/\n/g; print $myfile' < file.txt
Produces this:

Code:
asdf
asdf
asdf
asdf
asdf
vbmn
xcv
That was the desired output

Thanks again for the help!!
Attached Files
File Type: txt file.txt (64 Bytes, 4 views)
 
Old 02-26-2012, 06:29 AM   #8
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Ahh ok, I see now. That was just a misunderstanding then.... Well at least we figured it out though. Cheers once again!

Josh
 
  


Reply

Tags
sed


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
How do I replace the text between patterns located on separate lines? (sed, awk, etc) Quon Programming 5 02-12-2012 06:27 AM
[SOLVED] sed - replace alternate occurrences (on different lines) gazzatav Programming 5 04-16-2011 11:46 AM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
need sed help - how to replace all instances of X except those on lines with Y? BrianK Programming 4 03-25-2008 06:49 PM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM


All times are GMT -5. The time now is 06:12 AM.

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