LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to find and replace a text spanning multiple lines with sed (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-and-replace-a-text-spanning-multiple-lines-with-sed-931349/)

haveanother 02-26-2012 04:54 AM

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?

corp769 02-26-2012 04:57 AM

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

Cheers,

Josh

haveanother 02-26-2012 05:43 AM

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

corp769 02-26-2012 05:58 AM

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

haveanother 02-26-2012 06:02 AM

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!

corp769 02-26-2012 06:07 AM

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

haveanother 02-26-2012 06:11 AM

1 Attachment(s)
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!!

corp769 02-26-2012 06:29 AM

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

Josh


All times are GMT -5. The time now is 11:52 PM.