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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-26-2012, 04:54 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2011
Posts: 13
Rep:
|
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?
|
|
|
|
02-26-2012, 05:43 AM
|
#3
|
|
LQ Newbie
Registered: Jan 2011
Posts: 13
Original Poster
Rep:
|
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.
|
|
|
|
02-26-2012, 05:58 AM
|
#4
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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
|
|
|
|
02-26-2012, 06:02 AM
|
#5
|
|
LQ Newbie
Registered: Jan 2011
Posts: 13
Original Poster
Rep:
|
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!
|
|
|
|
02-26-2012, 06:07 AM
|
#6
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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
|
|
|
|
02-26-2012, 06:11 AM
|
#7
|
|
LQ Newbie
Registered: Jan 2011
Posts: 13
Original Poster
Rep:
|
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!!
|
|
|
|
02-26-2012, 06:29 AM
|
#8
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
Ahh ok, I see now. That was just a misunderstanding then.... Well at least we figured it out though. Cheers once again!
Josh
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:30 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|