用sed递归替换django模板标签

时间:2022-07-24 16:53:10

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.

我在django应用程序中重命名了一些东西,我希望以递归方式搜索并替换所有模板中的标记。我尝试使用find和sed这样做。

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.

好的,所以我尝试了一大堆不同的东西,试着让它发挥作用。我尝试了脱臼和双重逃避花括号。我尝试使用[\ t]而不是\ s。似乎没什么用。某些组合不会出错,但它们也找不到或替换任何内容。甚至更糟的是有时我得到另一个错误。

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.

路径如何在表达式之前? 。是路径,它紧跟在find命令之后。它先于所有表达式。

2 个解决方案

#1


1  

Try:

find . -name '*.html' -exec sed -i 's|{{\s*oldtag\s*}}|{{ newtag }}|g' {} +

With some assumptions:

有一些假设:

  • your sed implementation recognizes the \s escape sequence and the -i option
  • 你的sed实现识别\ s转义序列和-i选项

  • your find implementation supports the {} + syntax
  • 您的find实现支持{} +语法

#2


0  

You should be escaping the ' and \ characters. This should work:

你应该逃避'和\字符。这应该工作:

find . -name *.html -exec sed -i \'s/{{\\s*oldtag\\s*}}/{{ newtag }}/g\' {} \;

Tip: You can always just insert echo just before the word sed to see a printout of what it looks like (see what is escaped).

提示:您可以随时在单词sed之前插入echo以查看其外观的打印输出(请参阅转义的内容)。

#1


1  

Try:

find . -name '*.html' -exec sed -i 's|{{\s*oldtag\s*}}|{{ newtag }}|g' {} +

With some assumptions:

有一些假设:

  • your sed implementation recognizes the \s escape sequence and the -i option
  • 你的sed实现识别\ s转义序列和-i选项

  • your find implementation supports the {} + syntax
  • 您的find实现支持{} +语法

#2


0  

You should be escaping the ' and \ characters. This should work:

你应该逃避'和\字符。这应该工作:

find . -name *.html -exec sed -i \'s/{{\\s*oldtag\\s*}}/{{ newtag }}/g\' {} \;

Tip: You can always just insert echo just before the word sed to see a printout of what it looks like (see what is escaped).

提示:您可以随时在单词sed之前插入echo以查看其外观的打印输出(请参阅转义的内容)。