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 05-11-2017, 06:54 AM   #1
mierdatuti
Member
 
Registered: Aug 2008
Posts: 61

Rep: Reputation: 15
replace multiple lines with sed


Hi,
I have a file with thse code
Code:
*

*
I would like to replace all ocurrences in the text file with sed but when I do:
Code:
sed 's/*\n\n*\n\n/----------------/g' example
Don't works. Somebody could help me please?
Thanks and sorry for my English!
 
Old 05-11-2017, 07:06 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,782

Rep: Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573
sed is a stream editor - it only deals with "records"; you get the data up to but not including the newline (\n).
Also the "g" modifier only applies to the current record.

It is possible to read several records in then run the substitution. The following code searches for the "*" then reads the next 2 lines, then replaces the lot with dashes similar to what you did.
Code:
sed '/*/ {N;N;s/.*/----------------/}' example

Last edited by syg00; 05-11-2017 at 10:04 PM. Reason: cleanup
 
1 members found this post helpful.
  


Reply


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 to replace below lines with mutliple occuences using sed gurumoha Programming 3 06-26-2013 07:08 AM
[SOLVED] How to find and replace a text spanning multiple lines with sed haveanother Linux - Newbie 7 02-26-2012 06:29 AM
[SOLVED] sed - replace alternate occurrences (on different lines) gazzatav Programming 5 04-16-2011 11:46 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:50 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
Open Source Consulting | Domain Registration