[英]How to replace text in a bash script between specific characters with content from another file?
我的问题与另一个已发布的问题类似:但我只需要用另一个文件中的代码替换两个HTML注释之间的代码(包括两个html注释(...))即可。
例如,在aSample.htm中,我想替换“ test1”注释之间的代码:
<!-- test1 Begin -->
<link rel="stylesheet" type="text/css" href=
"http://www.aServer.com/template/style.css">
<!-- test1 End -->
带有来自另一个文件的代码(例如template.txt):
<!-- Changes Begin -->
<link rel="stylesheet" type="text/css" href=
"/srv/anotherServer.com/htdocs/api/test/cfHtmlHeadTest.css">
<!-- Changes End -->
到目前为止,我的脚本将从指定的IP地址($ ip)中获取所有html文件,并为它们创建一个新目录。 这是我尝试根据另一个搜索的网站进行替换的内容:
#!/bin/bash
TEST_FILE=$(cat./template.txt)
TARGETFILE='app/HtmlPages/"$ip-$currentTime"/"$ip"/index.php/art/*.htm'
STARTTAG="test1 Begin"
ENDTAG="test1 End"
# stuff
# Replaces all htm files' content between $START-/ENDTAG by inserting line from template.txt
echo "css replacement test..."
sed -e "s&\(<\!\-\-\$STARTTAG\-\->\).*\(<\!\-\-\$ENDTAG\-\->\)&\1$TEST_FILE\2&"
在添加此更改之前,代码按预期运行。 但是,当我进行更改时,系统会冻结或需要很长时间才能处理,对于如此小的更改,我并不期望。 我尝试了其他sed命令(例如, 该网站的变体),但是我得到了“'s'的未知选项” ...
我是否遗漏了一些东西,或者有人知道系统在做什么?
使用sed的r
命令在适当的时间读取文件的内容。
$ cat aSample.htm
foo
<!-- test1 Begin -->
<link rel="stylesheet" type="text/css" href=
"http://www.aServer.com/template/style.css">
<!-- test1 End -->
bar
$ cat template.txt
<!-- Changes Begin -->
<link rel="stylesheet" type="text/css" href=
"/srv/anotherServer.com/htdocs/api/test/cfHtmlHeadTest.css">
<!-- Changes End -->
$ sed '
/<!-- test1 End -->/ r template.txt
/<!-- test1 Begin -->/,/<!-- test1 End -->/ d
' aSample.htm
foo
<!-- Changes Begin -->
<link rel="stylesheet" type="text/css" href=
"/srv/anotherServer.com/htdocs/api/test/cfHtmlHeadTest.css">
<!-- Changes End -->
bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.