【发布时间】:2018-06-22 18:19:50
【问题描述】:
我正在尝试编写一个 bash 脚本,该脚本将在给定的 HTML 文件中进行搜索,定位是否有任何没有 FQDN 的 CSS 样式引用并将其添加到行内。
例如:
将href="css/style.css" 替换为href="http://my.domain/css/style.css"
(当然目录并不总是“css”。可以是其他任何东西......)
谢谢
【问题讨论】:
我正在尝试编写一个 bash 脚本,该脚本将在给定的 HTML 文件中进行搜索,定位是否有任何没有 FQDN 的 CSS 样式引用并将其添加到行内。
例如:
将href="css/style.css" 替换为href="http://my.domain/css/style.css"
(当然目录并不总是“css”。可以是其他任何东西......)
谢谢
【问题讨论】:
[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/ ......
与您选择的模式。 现在您可以根据自己的需要进行调整
【讨论】:
$ 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"
【讨论】: