3

I work with a lot of Matlab figures which were exported to SVG-files. Matlab produces a lot of garbage alongside the actual image (mainly invisible lines). So there is a lot to clean up after importing an image to have a clean .pdf_tex export for an inclusion in my Latex document. I do this cleaning in the XML editor window by deleting any object which is not part of the actual image (click on it and press delete, wastes a lot of time).

After importing several SVGs, my ID numbers in Inkscape a getting very high with big gaps in between (e.g. 9229, 10503, 24219 ...). So I wondered if it's possible to reset them to have consecutive numbers?

It would really facilitate that cleaning process I described above.

Thanks for your help!

2

You can perform the task with a simple awk-script.

Using a sample svg-file, searching for 'id=' I get:

grep "id=" sample.svg 
   id="svg2"
     id="metadata8">
     id="defs6">
       id="clipPath3342">
         id="circle3344"
     id="namedview4"
     showgrid="false"
     id="image10"
     id="path3348"
     id="image3338"
     id="path3337"

Now, with the awk-Script:

awk 'BEGIN { n=1;}  /(\Wid=[^0-9]+)/ { sub(/[0-9]+/, n, $0) 
++n
} 
/.*/ { print }
' sample.svg | grep "id="

I get

   id="svg1"
     id="metadata2">
     id="defs3">
       id="clipPath4">
         id="circle5"
     id="namedview6"
     showgrid="false"
     id="image7"
     id="path8"
     id="image9"
     id="path10"

Note, that 'showgrid' isn't touched. The grep command is used to supress all lines without 'id=' in them.

Awk is part of most unixoid systems and normally part of every Linux distribution, sometimes as gawk, mawk, etc. I guess it is part of OSX too and I know it is available for multiple Windows versions, but not part of them. But since it is open source, it is available for free.

To get the corrected file, pipe the output to a new file:

awk CODE sample.svg > corrected.svg

If you write the awk-code between the apostrophs to a file, you call it like this:

awk -f code-file.awk sample.svg > corrected.svg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.