嗨有可能使用包含在bash脚本中的正则表达式来编辑.html文件吗?使用正则表达式使用批处理脚本编辑index.html文件
这里就是我想要做的事:
replaceText="<a href="some-file-here" id="text">link to the new file</a>"
#open index.html file stream(how?)
#do some if condition that meets the regex below:
IF index.html contains <td abbr="fileOne">(.*)</td>
Index.html replaceText
我是很新的bash脚本,但我想知道如果以上是可能的吗?
这对OSX UNIX和Linux都有效。
下面是index.html的例子:
<html>
<head>
</head>
<body style="width: 50%; height: 50%;">
<div style="top: 10%; left: 10%; position: absolute;">
<img border="0" src=“icon.png” alt="Hello World" width="120" height="120">
<table style="width:300px">
<tr>
<td abbr=“file one”><a href=“someFile” id="text">Install file one here…</a></td>
<td abbr=“fileTwo”><a href=“someFileTwo” id="text">install file Two here…</a></td>
<td></td>
</tr>
</table>
</div>
</body>
</html>
在此先感谢
编辑:我试图用sed命令是
sed -i.bak 's/<td abbr="fileOne">(.*?)<\/td>/WHAT_YOU_WANT/' index.html
但是我得到上面的错误,当我打开.bak文件:
syntax error near unexpected token `newline'
你是指批处理脚本还是bash脚本?你说你想让你的解决方案在osx unix和linux上工作,但批处理文件只能用于Windows? –
对不起bash脚本。必须在unix/linux – jonney
上使用sed,POSIX兼容sed在所有这些系统上都可用,请参阅答案。 (假设你想替换与你的replace_text相匹配的行,这是问题似乎在问) –