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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SVG File search and replace string

Status
Not open for further replies.

iceolated

Technical User
Aug 8, 2010
3
US
I need to read in a svg file, search for a specific string and replace it with a string I have stored as a variable, and save the new svg with a new file name.

can anyone give me a few suggestions on how to get started?

I am beginning to experiment with SVG::parser, but I fear that there is an extremely simple solution that I am over-complicating.

any suggestions/snippets would be most appreciated
 
Hey,

I'm not familiar with SVG files, but from looking over the documentation for SVG::parser, I don't think its what you are looking for:

From documention said:
It takes XML as input and produces an SVG object as its output.

SVG::DOM ( allows you to manipulate SVG files through DOM like methods, and seems like a good option.

It may just be easier to open the SVG file, read it line by line, search for a string, replace it with a new string if need be, write out to a new file.

Chris
 
Here is a sample svg I will be working with. My problem is literally as simple as replacing 'DESCRIPTION' and 'SHEET' with different text that I have stored in an array and saving it as a new file.

<?xml version="1.0" encoding="UTF-16" ?>
<svg

height="3520"
width="5440"
>
<style
classname="EntityStyle"
id="2"
type="text/css"
>
font-size: 22; stroke: none }
text.function { fill: black; font-family: Arial; font-size: 22; stroke: none }

</style>
<g
id="3"
cb-objectname="Comments"
>
<rect
id="7a"
class="annotation"
height="0"
width="0"
x="3020"
y="1890"
/>
<line
classname="LineEntity"
id="8J"
class="annotation"
x1="3710"
x2="3710"
y1="1330"
y2="2730"
/>

<text
classname="TextEntity"
id="8a"
baseline-shift="0%"
class="annotation"
fill="#000000"
font-family="xxxx"
font-size="8"
x="390"
y="1370"
>
SHEET
</text>
<text
classname="TextEntity"
id="8s"
baseline-shift="0%"
class="annotation"
fill="#000000"
font-family="xxx"
font-size="8"
x="1270"
y="1370"
>
DESCRIPTION
</text>

</svg>
 
Are they single lines that you are replacing? If so, it should be as simple as:

Code:
perl -pe 's/SHEET/NEW_SHEET/;s/DESCRIPTION/NEW_DESCRIPTION/;' original_file > new_file

Or is there more to it than that?

Annihilannic.
 
The actual svg will be much larger. I will end up replacing the text that comes in-betwenn <text> </text> around 80 times per svg file and I will have about 100 different svgs
 
That's fine... but is each individual replacement just a few words on a line, or a whole bunch of lines?

The following will do a similar thing to multiple files, keeping a backup of each (called originalfilename.bak):

Code:
perl -p -i.bak -e 's/SHEET/NEW_SHEET/;s/DESCRIPTION/NEW_DESCRIPTION/;' *.svg

Annihilannic.
 
Did op notice what he posted is not even a well-formed svg? If it is not well-formed at that level, SVG::parser and SVG::DOM won't help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top