我想通过命令行在HTML文件上运行查找和替换。

我的命令看起来像这样:

sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html > index.html

当我运行它并在之后查看该文件时,它是空的。它删除了我文件的内容。

当我再次恢复文件后运行这个:

sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html

stdout是文件的内容,并且已经执行了查找和替换。

为什么会这样?


当前回答

另一种有用的模式是:

sed -e 'script script' index.html > index.html.tmp && mv index.html.tmp index.html

这具有大致相同的效果,但不使用-i选项,并且还意味着,如果sed脚本由于某种原因失败,输入文件也不会被破坏。此外,如果编辑成功,就不会留下备份文件。这种习惯用法在makefile中很有用。

很多seed都有-i选项,但不是所有的;posix sed则不是。因此,如果您的目标是可移植性,最好避免使用这种方法。

其他回答

使用sed的-i选项,例如:

sed -i bak -e s/STRING_TO_REPLACE/REPLACE_WITH/g index.html

要更改多个文件(并将每个文件的备份保存为*.bak):

perl -p -i -e "s/\|/x/g" *  

将采取所有文件在目录和替换|与x 这就是所谓的“Perl饼”(简单如饼)

ed的答案是:

printf "%s\n" '1,$s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' w q | ed index.html

为了重申codaddict的回答,shell首先处理重定向,清除“input.html”文件,然后调用“sed”命令,向其传递一个现在为空的文件。

警告:这是一个危险的方法!它滥用了linux中的i/o缓冲区,通过特定的缓冲选项,它可以处理小文件。这是一件有趣的奇事。但是不要在真实的情况下使用它!

除了sed的-i选项 您可以使用tee实用程序。

从男人:

Tee -从标准输入读取并写入标准输出和文件

所以,解决方案是:

sed s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html | tee | tee index.html

-- here the tee is repeated to make sure that the pipeline is buffered. Then all commands in the pipeline are blocked until they get some input to work on. Each command in the pipeline starts when the upstream commands have written 1 buffer of bytes (the size is defined somewhere) to the input of the command. So the last command tee index.html, which opens the file for writing and therefore empties it, runs after the upstream pipeline has finished and the output is in the buffer within the pipeline.

下面的方法很可能行不通:

sed s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html | tee index.html

-- it will run both commands of the pipeline at the same time without any blocking. (Without blocking the pipeline should pass the bytes line by line instead of buffer by buffer. Same as when you run cat | sed s/bar/GGG/. Without blocking it's more interactive and usually pipelines of just 2 commands run without buffering and blocking. Longer pipelines are buffered.) The tee index.html will open the file for writing and it will be emptied. However, if you turn the buffering always on, the second version will work too.

sed -i.bak "s#https.*\.com#$pub_url#g" MyHTMLFile.html

如果你有一个链接要添加,试试这个。按照上面的方法搜索URL(这里以https开始,以。com结束),并将其替换为URL字符串。我在这里使用了一个变量$pub_url。S表示搜索g表示全局替换。

真的有用!