Register
It is currently Wed Nov 26, 2014 1:37 am

Adding dtd- and css-reference to xml-files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Sep 16, 2009 1:13 pm   

Joined: Wed Sep 16, 2009 1:01 pm
Posts: 2
Hi!

I have some xml-files without reference to a dtd and css. I want to add such a reference, and thought of a script like:

#!/bin/sh
for i in *.xml
do sed {s/<?xml version="1.0" encoding="ISO-8859-1"?>/`<?xml version="1.0" encoding="ISO-8859-1"?>\n<!DOCTYPE inventory SYSTEM "file.dtd">\n<?xml-stylesheet type="text/css" href="file.css"?>`}
done

(Replacing the first line of each file with itself and two other lines.)
However, I get an error that the map or file doesn't exist.
What would be the right syntaxis?


Top
 Profile  
 PostPosted: Thu Sep 17, 2009 4:22 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Well sed is not built to operate without input data...

This is one method
Code:
$ echo "test data" | sed "s/test \(data\)/\1/"
data


Another is
Code:
$ echo "test data" > tmp.txt
$ sed -e "s/test \(data\)/\1/" tmp.txt
data


Both these methods will just output the replaced lines. The second method can be altered to change the files on-the-fly by adding -i instead of -e or to create a new file (still using -e) and just redirecting output to a new filename (> tmp2.txt)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Sep 17, 2009 1:17 pm   

Joined: Wed Sep 16, 2009 1:01 pm
Posts: 2
Fredrik, thanks!

I found it difficult to figure out how to interpret "test data", "test \(data\)" or "\1", but I also found out that I was too much orientated at a bash script that I (with some help) had written almost two years ago, while this could be done with a single command.

In the end, the command looked like:

Code:
sed -i 's/<?xml version="1.0" encoding="ISO-8859-1"?>/<?xml version="1.0" encoding="ISO-8859-1"?>\n<!DOCTYPE something SYSTEM "file.dtd">\n<?xml-stylesheet type="text\/css" href="file.css"?>/' *


Top
 Profile  
 PostPosted: Fri Sep 18, 2009 7:28 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Nico wrote:
I found it difficult to figure out how to interpret "test data", "test \(data\)" or "\1", but I also found out that I was too much orientated at a bash script that I (with some help) had written almost two years ago, while this could be done with a single command.


Sorry about that. It's like second nature for me so I miss to explain it more in-depth sometimes :)

"test data" is just some random text string to run with sed.
\(data\) is a regexp buffer command. It will save "data" from the string matched ("test data" in this case).
and \1 is just the position of the buffer. If you add 2 \(<something>\) then the first is \1 and the second is \2

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP