LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-09-2010, 07:00 AM   #1
xexers
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
How can I use the command line to split a single-lined XML into a multi-line XML


Hi,

I have a many text files that have XML tags all shoved into 1 line. I want to create a new file that splits each XML tag onto a new line.

Example

Input: <Company></Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>54</CharactersWithSpaces><SharedDoc>false</SharedDoc>


Desired output:

<Company></Company>
<LinksUpToDate>false</LinksUpToDate>
<CharactersWithSpaces>54</CharactersWithSpaces>
<SharedDoc>false</SharedDoc>


I would like this to run in a shell script.
Any ideas?

Thanks!
 
Old 12-09-2010, 07:12 AM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
You could use sed to do this. I'm sure there are many ways to accomplish this, here is one quick way given the data set that you have provided:

Code:
sed 's/><[^/]/>\n</g' file.txt

Regards,

Fordeck

Last edited by fordeck; 12-09-2010 at 07:15 AM. Reason: correction
 
Old 12-09-2010, 07:17 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You could match your tag pairs using a regular expression like this:
Code:
perl -pe 's%(<[^>]*>[^<]*</[^>]*>)%$1\n%g' <input.txt >output.txt
but it will start getting overly complicated if you have to deal with nested tags and so on.

A better alternative would be to use an existing xml formatting tool such as xmlindent.

Last edited by neonsignal; 12-09-2010 at 07:43 AM.
 
1 members found this post helpful.
Old 12-09-2010, 07:25 AM   #4
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
I should have tested before I posted. The one line sed command is not doing what you asked at least not without some additional corrections. The previous post from "neonsignal" is a much better solution.

Sorry for the incorrect post.

Regards,


Fordeck
 
  


Reply



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] split multi line record into multiple files with awk pcock Programming 10 11-12-2009 04:12 AM
Reformat 'pretty' xml to one-line entries gnashley Programming 2 06-23-2008 04:34 PM
Good command line utility for XML to search, edit, etc. ? lumix Linux - Software 1 05-05-2008 10:56 AM
How to remove single-line XML nodes from files dfrechet Programming 1 10-11-2005 02:00 PM
PHP - XML error: junk after document element at line... jimieee Programming 2 04-27-2004 11:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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