Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to parse XML file with no newline char. Want to add newline

Status
Not open for further replies.

eholdo

Technical User
May 30, 2002
31
US
Using c-shell, I need to do the following:

I have an XML file that has <tag>data</tag><tag2>more data</tag2>

etc..... This is all on one long line, or multiple lines if it goes past 1024 characters or so. I need to work with this file to parse these values, and want to break the file up wherever there is a >. In other words, every > I want to replace with a \n (LF.

Help!!!

Thanks
Erik
 
Erik,

This sed statement will give you the following -

<tag>
data</tag>
<tag2>
more data</tag2>


sed 's/>/>\n/g' datafile


Note - There will be an extra newline at the end of the data.


Hope this helps,

Steve
 
Thanks Steve,

I've been trying this, but for some reason, the \n is not being interpreted and it's just replacing the > with >n

Any thoughts. I'm on Solaris 2.6

Thanks again.
 
Make sure there are no spaces between the single quotes.

The sed -e switch might be an option as well but I know the \n is the newline syntax for Solaris as well as Linux.

Let me know if it still doesn't work I will get access to a Solaris server to make sure I am not missing something.
 
Still don't know why it's not working, but I used:

cat filename | tr &quot;>&quot; &quot;\n&quot; > newfilename

and that worked.

Thanks for the effort. I think it may be my version of sed.

 
You can use awk :

awk '{gsub(&quot;>&quot;,&quot;>\n&quot;) ;sub(&quot;\n$&quot;,&quot;&quot;) ; print $0}'

For every line :
[tt]gsub(&quot;>&quot;,'>\n&quot;)[/tt] add newline after every '>'
[tt]sub(\n$&quot;,&quot;&quot;) [/tt] remove extra newline added for lines ending with '>'
[tt]print $0 [/tt] print the line


Jean Pierre.
 
With sed :

--- break.sed ---
s/>/>/g
------------------

sed -f break.sed < input_file > output_file

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top