2017-07-16 79 views
-1

我有一个svg文件的文件夹,称之为svg。在很多SVG文件的根目录中添加属性

为他们每个人的第一行,看起来像:

<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg" height="296.90mm" version="1.2" viewBox="0 0 118.6170 167.6220" width="210.10mm"> 

我想将它更改为:

<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg" height="296.90mm" version="1.2" viewBox="0 0 118.6170 167.6220" width="210.10mm" shape-rendering="crispEdges"> 

所以,整个的一点是添加形状渲染=” crispEdges“到每个文件。但是,我有很多这些文件(数十万),所以我需要它尽可能高效。

有没有人知道这样做的快速方法,无论是从python或从Ubuntu终端?

谢谢!

+0

你必须证明你自己的努力来解决它......足以与'sed' ..你可以指定你只需要改变一号线,使用正则表达式来代替简单的'>'在所需字符串的行末,使用'-i'选项执行就地编辑等等......请参阅https://stackoverflow.com/tags/sed/info – Sundeep

+0

我之前没有使用过这些工具。我可以用Python做,但对我来说效率不高。 – TheRevanchist

+0

然后在这里添加Python代码..如果有问题,我确定你会在这里得到帮助...如果它运作良好,但你想知道改进的地方,请访问https://codereview.stackexchange.com/ ...''以前没有使用这些工具,那么你将不得不开始学习...... – Sundeep

回答

1

很可能sed是一种最适合您的用途的工具。

sed -e -i 's/<ns0:svg\(.*\)>/<ns0:svg\1 shape-rendering="crispEdges" >/g' <files_you_want_to_update> 
+0

这似乎不适用于我。它不能识别-e命令,没有它就说没有输入文件(尽管我在有svg文件的文件夹中)。 – TheRevanchist

+0

@TheRevanchist更新了答案,您需要指定要更新的文件。 –

+0

酷,我改变了你的代码遍历文件,它的工作就像一个魅力。我将解决方案放在OP中(因为这里的格式不太好),并接受你的答案。 感谢您的帮助! – TheRevanchist

1

如果您对awk还可以,那么您也可以尝试下面的内容。

awk -v s1="shape-rendering=\"crispEdges\"" '{sub(/>$/,FS s1 ">")} 1' Input_file > temp_file && mv temp_file Input_file 

编辑:添加的解释与解决非班轮一个形式太在这里。

awk -v s1="shape-rendering=\"crispEdges\"" '{      ##creating variable named s1 which will have value of string mentioned by OP. 
               sub(/>$/,FS s1 ">")##substituting last occurrence with FS s1 > with it in the line. 
              } 
              1      ##Writing 1 means making the condition TRUE and NOT mentioning any action here so by default print of current line will happen. 
              ' Input_file > temp_file \ ## Writing output of this into a temp_file then continuing the following command. 
              && mv temp_file Input_file## && means if previous commands is SUCCESSFUL then rename temp_file to Input_file 

EDIT2:按OP的要求,我根据.SVG文件改变我的代码,它会保存单个INPUT_FILE本身的变化(如果有的话)。

awk -v temp_file="file" -v s1="shape-rendering=\"crispEdges\"" 'FNR==1 && f{ 
                ####print "f= "f,"filename= " FILENAME 
                close(temp_file); 
                close(f); 
                system("mv "temp_file FS f); 
                f="" 
                } 
             /ns0:svg xmlns:ns0=\"w3.org\/2000\/svg\"/ 
             { 
              sub(/>$/,FS s1 ">") 
             } 
             {if(!f) 
             { 
              f=FILENAME 
             }} 
             { 
              print > temp_file; 
             } 
             ' *.svg 
+0

如何使用svg文件为整个文件夹执行此操作?对不起,我还没有使用这些工具,我不知道你是否可以迭代文件。 – TheRevanchist

+0

我可以编辑我的文章,如果你想这样做的特定文件,我的问题是你想要保存更改到Input_file本身?请让我知道。 – RavinderSingh13

+0

是的,我希望将更改保存在输入文件中。所以,在流程结束时,应该有相同的文件,但每个文件都应该有这个额外的属性。 – TheRevanchist

0

(发布代表OP)的

感谢A. Haaji的回复,问题解决了。我只需遍历一个文件列表,然后使用他们的代码,一切正常。最终的解决方案是:

for i in *.svg 
do 
    sed -i 's/<ns0:svg\(.*\)>/<ns0:svg\1 shape-rendering="crispEdges" >/g' "$i" 
done 
相关问题