Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 12-09-2010, 07:00 AM   #1
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


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.


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

Desired output:


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

Old 12-09-2010, 07:12 AM   #2
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:

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



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

Rep: Reputation: 359Reputation: 359Reputation: 359Reputation: 359
You could match your tag pairs using a regular expression like this:
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
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.




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] 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 > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration