LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2018, 02:20 AM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mageia
Posts: 321

Rep: Reputation: 30
sed add carriage return after pattern


I have been trying to add a carriage return after the pattern </ele> (which has special characters) in the sample line:

<ele>66.47</ele><ele>66.47</ele><ele>66.47</ele><ele>66.47</ele>

to produce:

<ele>66.47</ele>
<ele>66.47</ele>
<ele>66.47</ele>
<ele>66.47</ele>

I have tried:

Code:
sed -e "s/ \</ele>/ & \n/g"  file1.txt  file2.txt
What do I need to do to enclose the characters in </ele> ?
 
Old 07-12-2018, 02:48 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 3,197
Blog Entries: 3

Rep: Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407
Quote:
Originally Posted by Johng View Post
What do I need to do to enclose the characters in </ele> ?
You're close but you have a lot of extra spaces in the pattern, so it won't match. Then once those are removed you need to either escape the slash / or use another separator for the substitution s/// command.

Code:
sed -e "s/<\/ele>/&\n/g" file1.txt file2.txt

sed -e "s#</ele>#&\n#g" file1.txt file2.txt
sed -e "s|</ele>|&\n|g" file1.txt file2.txt
sed -e "s%</ele>%&\n%g" file1.txt file2.txt
See "man sed"

If you are dealing with XML and your tasks get even slightly more complex, it would be highly recommended to use a real XML parser. There are many to choose from but I prefer those in in Perl 5's CPAN. XML::Parser and XML::XPath are two to start with.
 
Old 07-12-2018, 02:59 AM   #3
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mageia
Posts: 321

Original Poster
Rep: Reputation: 30
Thank you, that looks like it did the job.
 
  


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
[SOLVED] sed to add between two pattern essahar Programming 6 04-08-2012 05:11 AM
[SOLVED] search, duplicate and add pattern with sed/awk lolworlds Programming 4 02-03-2012 10:17 AM
[SOLVED] add texts using sed in matching pattern manya Programming 5 02-03-2011 07:59 AM
carriage return mike2008 Linux - Newbie 3 10-15-2009 12:03 AM
Using sed to add carriage retuns and line numbers. Optimistic Programming 11 04-08-2005 01:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration