查找并替换特定目录中的文件中的字符串

时间:2022-10-06 19:19:52

I have a pattern that I need to replace in my .hpp, .h, .cpp files in multiple directories.

我有一个模式,我需要在多个目录中的.hpp,.h,.cpp文件中替换。

I have read Find and replace a particular term in multiple files question for guidance. I am also using this tutorial but I am not able achieve what I intend to do. So here is my pattern.

我已阅读查找并替换多个文件问题中的特定术语以获取指导。我也在使用本教程,但我无法实现我打算做的事情。所以这是我的模式。

throw some::lengthy::exception();

I want to replace it with this

我想用它替换它

throw CreateException(some::lengthy::exception());

How can I achieve this?

我怎样才能做到这一点?

UPDATE:

Moreover, what if the some::lengthy::exception() part is variant such that it changes for every search result ? Something like

如果some :: lengthy :: exception()部分是变体,它会为每个搜索结果而改变怎么办?就像是

throw some::changing::text::exception();

will be converted to

将被转换为

throw CreateException(some::changing::text::exception());

3 个解决方案

#1


You can use the sed expression:

你可以使用sed表达式:

sed 's/throw some::lengthy::exception();/throw CreateException(some::lengthy::exception());/g'

And add it into a find command to check .h, .cpp and .hpp files (idea coming from List files with certain extensions with ls and grep):

并将其添加到find命令中以检查.h,.cpp和.hpp文件(来自带有ls和grep的某些扩展名的List文件的想法):

find . -iregex '.*\.\(h\|cpp\|hpp\)'

All together:

find . -iregex '.*\.\(h\|cpp\|hpp\)' -exec sed -i.bak 's/throw some::lengthy::exception();/throw CreateException(some::lengthy::exception());/g' {} \;

Note the usage of sed -i.bak in order to to edits in place but create a file.bak backup file.

请注意sed -i.bak的使用,以便在适当的位置编辑,但创建一个file.bak备份文件。

Variable pattern

If your pattern varies, you can use:

如果您的模式不同,您可以使用:

sed -r '/^throw/s/throw (.*);$/throw CreateException(\1);/' file

This does the replacement in the lines starting with throw. It catches everything after throw up to ; and prints it back surrounded by CreateException();`.

这是以throw开头的行中的替换。它抛出之后就抓住了一切;并将其打印回来,包围CreateException();`。

Test

$ cat a.hpp throw some::lengthy::exception();throw you();asdfasdf throw you();$ sed -r '/^throw/s/throw (.*);$/throw CreateException(\1);/' a.hpp throw CreateException(some::lengthy::exception());throw CreateException(you());asdfasdf throw you();

#2


You could try the below sed command.

您可以尝试以下sed命令。

sed 's/\bthrow some::lengthy::exception();/throw CreateException(some::lengthy::exception());/g' *.cpp

Add inline-edit -i param to save the changes.

添加inline-edit -i param以保存更改。

#3


You can use the following:

您可以使用以下内容:

sed 's/\b(throw some::lengthy::exception());/throw CreateException(\1);/g'

#1


You can use the sed expression:

你可以使用sed表达式:

sed 's/throw some::lengthy::exception();/throw CreateException(some::lengthy::exception());/g'

And add it into a find command to check .h, .cpp and .hpp files (idea coming from List files with certain extensions with ls and grep):

并将其添加到find命令中以检查.h,.cpp和.hpp文件(来自带有ls和grep的某些扩展名的List文件的想法):

find . -iregex '.*\.\(h\|cpp\|hpp\)'

All together:

find . -iregex '.*\.\(h\|cpp\|hpp\)' -exec sed -i.bak 's/throw some::lengthy::exception();/throw CreateException(some::lengthy::exception());/g' {} \;

Note the usage of sed -i.bak in order to to edits in place but create a file.bak backup file.

请注意sed -i.bak的使用,以便在适当的位置编辑,但创建一个file.bak备份文件。

Variable pattern

If your pattern varies, you can use:

如果您的模式不同,您可以使用:

sed -r '/^throw/s/throw (.*);$/throw CreateException(\1);/' file

This does the replacement in the lines starting with throw. It catches everything after throw up to ; and prints it back surrounded by CreateException();`.

这是以throw开头的行中的替换。它抛出之后就抓住了一切;并将其打印回来,包围CreateException();`。

Test

$ cat a.hpp throw some::lengthy::exception();throw you();asdfasdf throw you();$ sed -r '/^throw/s/throw (.*);$/throw CreateException(\1);/' a.hpp throw CreateException(some::lengthy::exception());throw CreateException(you());asdfasdf throw you();

#2


You could try the below sed command.

您可以尝试以下sed命令。

sed 's/\bthrow some::lengthy::exception();/throw CreateException(some::lengthy::exception());/g' *.cpp

Add inline-edit -i param to save the changes.

添加inline-edit -i param以保存更改。

#3


You can use the following:

您可以使用以下内容:

sed 's/\b(throw some::lengthy::exception());/throw CreateException(\1);/g'