【发布时间】:2022-01-26 16:06:34
【问题描述】:
我有 2 个 html 文件。
- index.html 在名为“main”的文件夹中
- other.html 位于名为“other”的文件夹中
index.html 代码如下:
<h1>My first story</h1>
<p>The pen was blue.</p>
other.html 有以下代码:
<p>The pen was black.</p>
我想用 other.html 的全部内容替换“index.html”中的段落内容,以便 index.html 给出以下结果:
<h1>My first story</h1>
<p>The pen was red.</p>
我尝试使用终端命令cd main && sed -e "s_<p>The pen was blue.</p>_$(cd ../other && sed 's:/:\\/:g' other.html)_" index.html,但收到错误消息“bash: cd: main: No such file or directory”。即使我尝试将它们放在同一个目录中以进行测试,它也不起作用。但我需要将它们放在单独的文件夹中。
更新:我通过使用命令 sed -e "s_<p>The pen was blue.</p>_$(sed 's:/:\\/:g' other/other.html)_" main/index.html 修复了访问不同目录中文件的问题,但现在我收到错误消息 "sed: -e expression #1, char 54: unterminated `s' command"
如何让它将第二个文件的内容复制到第一个文件中?
【问题讨论】:
-
如果
cd main则没有其他方法可以工作,因此请先调试,然后在需要帮助时询问其余部分。 -
@EdMorton 我调试了“cd main”部分,但它仍然显示错误,因为它无法识别第二个文件的内容。
-
它是否必须被sed?为什么不使用python? | @edit
python -c 'stuff'和perl -pe 'stuff'也是一个“命令行”。基本上每个命令都是命令行 -echo "int main() { }" | gcc -xc - && ./a.out -
@KamilCuk 我想为此使用命令行。
-
stackoverflow.com/questions/407523/… 会回答您的问题吗?首先将文件读入字符串 - 然后转义。因为换行符链接到stackoverflow.com/questions/29613304/…。
标签: sed replace command-line