用 sed 递归替换 django 模板标签
我重命名了 django 应用程序中的某些内容,并且我想递归搜索并替换所有模板中的标签。我尝试像这样使用 find 和 sed 来做到这一点。
find . -name *.html -exec sed -i 's/\{\{\s*oldtag\s*\}\}/{{ newtag }}/g' {} \;
我收到这个错误。
sed: -e expression #1, char 44: Invalid preceding regular expression
好吧,所以我尝试了很多不同的方法来让它发挥作用。我尝试取消转义和双重转义花括号。我尝试使用 [ \t] 而不是 \s。似乎什么都不起作用。有些组合不会给出错误,但它们也不会找到或替换任何内容。更糟糕的是有时我会遇到另一个错误。
find: paths must precede expression: index.html
路径如何位于表达式之前? 。是路径,它紧跟在 find 命令之后。它位于所有表达式之前。
I renamed something in my django application, and I want to recursively search and replace the tag in all of the templates. I tried to do this using find and sed like so.
find . -name *.html -exec sed -i 's/\{\{\s*oldtag\s*\}\}/{{ newtag }}/g' {} \;
I get this error.
sed: -e expression #1, char 44: Invalid preceding regular expression
Ok, so I tried a whole bunch of different things to try to make it work. I tried unescaping and double-escaping the curly braces. I tried using [ \t] instead of \s. Nothing seems to work. Some of the combinations don't give an error, but they also don't find or replace anything. What's even worse is sometimes I get this other error.
find: paths must precede expression: index.html
How can the path precede the expression? . is the path, and it immediately follows the find command. It precedes all the expressions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
基于一些假设:
Try:
With some assumptions:
您应该转义
'
和\
字符。这应该有效:提示:您始终可以在单词
sed
之前插入echo
来查看其外观的打印输出(请参阅被逃脱)。You should be escaping the
'
and\
characters. This should work:Tip: You can always just insert
echo
just before the wordsed
to see a printout of what it looks like (see what is escaped).