递归多行 sed - 删除文件开头直到模式匹配
我有包含 html 文件的嵌套子目录。对于每个 html 文件,我想从文件顶部删除,直到模式
find . -name "*.html" -exec sed "s/.*?<div id=\"left-col/<div id=\"left-col/g" '{}' \;
我在终端中得到了大量 html 输出,但没有文件包含替换或被写入。
I have nested subdirectories containing html files. For each of these html files I want to delete from the top of the file until the pattern <div id="left-
This is my attempt from osx's terminal:
find . -name "*.html" -exec sed "s/.*?<div id=\"left-col/<div id=\"left-col/g" '{}' \;
I get a lot of html output in the termainal, but no files contain the substitution or are written.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的命令有两个问题。第一个问题是您没有为
sed
选择输出位置。第二个是您的 sed 脚本没有执行您希望它执行的操作:您发布的脚本将查看每一行并删除该行上的行将不受影响。您可能想尝试:
这还会通过将
.BAK
附加到原始版本来自动备份您的文件。如果不希望这样做,请将-i.BAK
更改为简单的-i
。There are two problems with your command. The first problem is that you aren't selecting an output location for
sed
. The second is that yoursed
script is not doing what you want it to do: the script you posted will look at each line and delete everything ON THAT LINE before the<div>
. Lines without the<div>
will be unaffected. You may want to try:This will also automatically back up your files by appending
.BAK
to the original versions. If this is undesirable, change-i.BAK
to simply-i
.当您想要将
sed
正则表达式的结果写入文件时,您将其输出到控制台stdout
。要使用 sed 执行查找和替换,请使用
-i
标志:如果可能,请确保在执行此命令之前备份文件。否则,您将面临因正则表达式输入错误而导致数据丢失的风险。
You're outputting the result of the
sed
regex tostdout
, the console, when you want to be writing it to the file.To perform find and replace with sed, use the
-i
flag:Make sure you backup your files before performing this command, if possible. Otherwise you risk data-loss from a mistyped regex.
您没有将 sed 的输出存储在任何地方;这就是为什么它会吐出 html 。
You're not storing the output of
sed
anywhere; that's why it's spitting out the html.