【问题标题】:sed to comment out css file with certain font-familysed 用某些字体系列注释掉 css 文件
【发布时间】:2014-02-11 20:05:34
【问题描述】:

我一直在寻找一种方法来注释.css 文件,该文件可能具有在@font-face#id、选择器和/或.class 中指示的特定字体系列。如果所有内容都与/foobar/ a\(append) 和/foobar/ i\(insert) 在一行上,我可以使用sed 执行此操作,但是如果所有内容都位于多行上,最好的方法是什么?

示例:

之前

@font-face {
    font-family: "foobar";
    src: url(fonts/foobar.ttf);
}

h1 {
       font-family: "foobar";
}

.foobar {
    font-family: "foobar";
}

最终结果:

/* @font-face {
    font-family: "foobar";
    src: url(fonts/foobar.ttf);
} */

/* h1 {
       font-family: "foobar";
} */

/* .foobar {
    font-family: "foobar";
} */

【问题讨论】:

    标签: css fonts sed multiline


    【解决方案1】:

    这可能对你有用(GNU sed):

    sed '/{/!b;:a;$!N;/}/!ba;/font-family:\s*"foobar";/s/.*/\/* & *\//' file
    

    【讨论】:

    • +1,这个想法很聪明。建议将替换命令改为s!.*!/* & */!',当替换字符为/
    【解决方案2】:

    可以用 perl 和 awk 做到这一点

    perl -00 -lpe '$_ = "/* $_ */" if /font-family:\s+.foobar/' file
    awk -v RS= '/font-family:[[:blank:]]+.foobar/ {$0 =  "/* " $0 " */"} 1' file
    

    这两种方法的工作方式相同:逐段读取文件(以空白行分隔),测试正则表达式,如果找到则添加注释标记。

    要就地编辑文件,您可以使用

    perl -i ...(as above)...
    
    awk ...(as above)... > tmpfile && mv tmpfile file
    

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 2014-09-13
      • 2018-02-19
      • 1970-01-01
      • 2013-01-08
      相关资源
      最近更新 更多