【问题标题】:Adding an attribute in the root of a lot of SVG files在许多 SVG 文件的根目录中添加一个属性
【发布时间】:2017-12-21 01:59:54
【问题描述】:

我有一个 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">

因此,重点是为每个文件添加 shape-rendering="crispEdges"。但是,我有很多这些文件(数十万个),所以我需要它尽可能高效。

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

谢谢!

【问题讨论】:

  • 你必须展示你自己的努力来解决它......使用sed很容易......你可以指定你只需要更改第一行,使用正则表达式替换&gt;在带有所需字符串的行尾,使用-i 选项执行就地编辑等等...参见stackoverflow.com/tags/sed/info
  • 我之前没用过这些工具。我可以在 Python 上做到这一点,但对我来说它看起来效率不高。
  • 然后在此处添加 Python 代码。如果有问题,我相信您会在此处获得帮助...如果它运行良好但您想知道需要改进的地方,请在 codereview.stackexchange.com 上提问...haven't used these tools before 那你就得开始学习了...

标签: python ubuntu svg sed terminal


【解决方案1】:

可能 sed 是最适合您的工具。

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

【讨论】:

  • 这似乎对我不起作用。它无法识别 -e 命令,没有它就表示没有输入文件(尽管我在有 svg 文件的文件夹中)。
  • @TheRevanchist 更新了一个答案,您需要指定要更新的文件。
  • 酷,我改变了你的代码来迭代文件,它的工作就像一个魅力。我将解决方案放在 OP 中(因为这里的格式看起来不太好),并接受您的回答。感谢您的帮助!
【解决方案2】:

如果你对 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

【讨论】:

  • 我怎样才能对包含 svg 文件的整个文件夹执行此操作?抱歉,我没用过这些工具,不知道你能不能用它们来遍历文件。
  • 如果您想对特定文件执行此操作,我可以编辑我的帖子,我的问题是您是否要将更改保存到 Input_file 本身?请让我知道。
  • 是的,我希望将更改保存在输入文件中。所以,在这个过程的最后,应该有相同的文件,但每个文件都应该有那个额外的属性。
  • @TheRevanchist:我已经更改了我现在在 EDIT2 中查看的代码,如果这对您有帮助,请告诉我。也请在您满意后尝试测试 1 或 2 个文件,然后您就可以满足真正的需求,让我知道它是如何进行的,干杯。
  • 您的代码有问题。它在任何地方都添加了 shape-rendering = crispEdges,而不仅仅是在第一行。看起来像: w3.org/2000/svg" height="296.90mm" version="1.2" viewBox="0 0 118.6170 167.6220" width="210.10mm" shape-rendering="crispEdges"> ...仅在第一行应添加属性。
【解决方案3】:

(代表 OP 发布).

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 2013-07-23
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多