【问题标题】:How to search html file for css reference without FQDN?如何在没有 FQDN 的情况下搜索 html 文件以获取 css 参考?
【发布时间】:2018-06-22 18:19:50
【问题描述】:

我正在尝试编写一个 bash 脚本,该脚本将在给定的 HTML 文件中进行搜索,定位是否有任何没有 FQDN 的 CSS 样式引用并将其添加到行内。

例如: 将href="css/style.css" 替换为href="http://my.domain/css/style.css"

(当然目录并不总是“css”。可以是其他任何东西......)

谢谢

【问题讨论】:

    标签: html css bash sed


    【解决方案1】:
        [root@h2g2w lib]# cat toto
        href=css/style.css
        href=other/style.css
        href=example/style.css
        href=css/style.css
        href=css/style.css
        href=css/style.css
    [root@h2g2w lib]# sed  -i "s/href=\(.*\)\/*.css/href=http:\/\/my.domain\/\1/" toto
    
    [root@h2g2w lib]# cat toto
    href=http://my.domain/css/style
        href=http://my.domain/other/style
        href=http://my.domain/example/style
        href=http://my.domain/css/style
        href=http://my.domain/css/style
        href=http://my.domain/css/style
    [root@h2g2w lib]# 
    
    [root@h2g2w lib]# 
    

    您需要将子目录名称隔离为搜索模式的子模式并在替换模式 (.*) 上重新粘贴它将粘贴为 \1 在替换/模式/替换/中的替换模式中

    在命令中:sed -i "s/href=\(.*\)\/*.css/href=http:\/\/my.domain\/\1/"

    用现有行的 href=http://my.domain/PASTE/end 替换 href=(copy)/whatever.css

    现在您可以根据自己的需要调整脚部

    在没有 http 的情况下搜索 lignes

    先于你的命令
    sed  ':v/http/ ......
    

    与您选择的模式。 现在您可以根据自己的需要进行调整

    【讨论】:

    • 谢谢!!这太棒了。
    • 只是另一个问题 - 以下脚本将添加 FQDN,即使它已经存在......这是一个问题 :) 有没有办法替换“href=”,只有在没有的情况下“http”参考?谢谢
    • 如前所述,通过 sed ':v/http/ 在你的命令之前搜索没有 http 的 lignes ......你必须自己适应我没有你的文件,所以我无法填满一个你可以复制/粘贴的脚本
    • 我只是给你一个我需要做的一切的例子:我有以下代码:--------- ------------- my.domain/css/main.css" />
    • 所以我希望脚本覆盖文档中的所有选项(忽略现有的 http 参考 | 为缺少的一个添加 FQDN | 还要考虑“.cs”文件)而且 - 如果我想为我的 FQDN 使用一个变量?例如使用 $1?所以我将按如下方式运行脚本:./myscript.sh my.domain 谢谢!
    【解决方案2】:
     $ sed -i 's|href=".*/style.css|href="http://my.domain/style.css|g' file.txt
    
    
     $ cat file.txt
     href="one/style.css"
     href="css/style.css"
     href="style/style.css"
     href="check/style.css"
     href="two/somethingelse"
    

    结果:

    $ sed 's|href=".*/style.css|href="http://my.domain/style.css|g' file.txt
    href="one/style.css"
    href="css/style.css"
    href="style/style.css"
    href="check/style.css"
    href="two/somethingelse"
    

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 1970-01-01
      • 2016-07-24
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多