我正在尝试编写一个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/"
substitue href=(copy)/whatever.css by href=http://my.domain/PASTE/end of existing line
现在您可以适应自己的需要
要搜索 lignes whtout http 在命令之前
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"