I have a sample svg file from the graphics program Inkscape. My objective is to collect every third pair from a very long line of floating point numbers. That seems simple enough. The larger objective is to write out svg code with calculated parameters - because Inkscape cannot do this in a "batch" mode - only "by hand" - all fine and doable.
I have a lengthy script (not shown) that gets the correct result, using bash, awk, grep, cut
and sed
- because I have a lot of such scripts to work from. I have looked at many other posts on Unix & Linux, which had some promise. Perhaps a single python
or perl
script would be better to write svg
output, but I am trying to focus on individual steps of the ad-hoc script that works - the initial grep/sed/awk
part - to make this appropriate for Stack Exchange.
The numbers are separated (if that is the correct word) by a comma, like this :
0.05306,-1.85438
I suppose I should show the line I am working on, apologies for the length. I am trying to preserve everything - including the leading spaces :
d="m 53.049246,151.83874 c 0.05306,-1.85438 0.155553,-3.63727 0.304925,-5.35109 0.153133,-1.75698 0.355536,-3.44137 0.604459,-
5.05582 0.257082,-1.66737 0.563784,-3.26013 0.917074,-4.78117 0.399178,-1.7186 0.857832,-3.34565 1.371592,-4.88532 0.566073,-1.69644
1.199045,-3.2868 1.893068,-4.77667 0.733396,-1.57438 1.534967,-3.03655 2.397812,-4.39309 0.897172,-1.4105 1.86059,-2.70681 2.882498,-
3.89632 1.158647,-1.34869 2.392483,-2.56009 3.690201,-3.64502 1.232816,-1.03066 2.523284,-1.94718 3.861711,-2.75883 1.457388,-0.88378
2.97164,-1.6432 4.53024,-2.29024 1.538451,-0.63866 3.120112,-1.16782 4.732946,-1.59897 1.636238,-0.4374 3.304561,-0.77392 4.992402,-
1.02158 1.640903,-0.24077 3.300254,-0.39755 4.966504,-0.48136 1.661534,-0.0836 3.329929,-0.0946 4.993735,-0.044 1.679345,0.0511 3.354
014,0.16488 5.012237,0.33022 1.63047,0.16257 3.24504,0.37495 4.83252,0.62644 1.7341,0.27473 3.43586,0.59612 5.09071,0.95025 1.65289,0
.35371 3.25896,0.74008 4.80369,1.14522 1.69385,0.44425 3.31394,0.91106 4.8411,1.38213 1.64731,0.50813 3.1865,1.02121 4.59351,1.51626
1.8366,0.6462 3.44798,1.26167 4.78064,1.79531 2.67181,1.06986 4.22329,1.81076 4.22329,1.81076"
... I note there are some letters in there too - in fact, the first pair has to be skipped - see the sed
command. So the next step in this script is to get the remaining pairs. The following works but seems a terrible way to do it - because ideally, the processing script should handle any svg
file Inkscape might produce :
input=for_script_test_01.svg
grep "\ d=" $input | sed 's/^.\{33\}//' | sed 's/\"//g' | \
awk '{print $3, $6, $9, $12, $15, $18, $21, $24, $27, $30, $33, $36, $39, $42, $45, $48, $51, $54, $57, $60, $63, $66, $69, $72, $75, $78}'
the output is :
0.304925,-5.35109 0.604459,-5.05582 0.917074,-4.78117 1.371592,-4.88532 1.893068,-4.77667 2.397812,-4.39309 2.882498,-3.89632 3.690201,-3.64502 3.861711,-2.75883 4.53024,-2.29024 4.732946,-1.59897 4.992402,-1.02158 4.966504,-0.48136 4.993735,-0.044 5.012237,0.33022 4.83252,0.62644 5.09071,0.95025 4.80369,1.14522 4.8411,1.38213 4.59351,1.51626 4.78064,1.79531 4.22329,1.81076
that is clumsy to read - and might have typos even though I checked - so I tried to put the pairs in a single column using a clumsy one-liner on the output file > output.dat
(note: the each $each
is a typo, should be echo $each
, but we are leaving it be):
user@local:/home$ my_list=`cat output.dat` ; for each in $my_list ; do each $each ; done
the last three lines of the output, to check :
4.59351,1.51626
4.78064,1.79531
4.22329,1.81076
... apologies for length. Hopefully the idea is clear.
UPDATE : this post on extracting path strings and lines from svg files using some svg parser modules in python will be helpful for working this out :
https://stackoverflow.com/questions/15857818/python-svg-parser
.
instead of a,
in theawk
call. I adjusted and added some output. \$\endgroup\$