查找并替换特定目录中的文件中的字符串 [英] find and replace strings in files in a particular directory
问题描述
我有一个模式,需要在 .hpp
, .h
, .cpp
在多个目录中的文件。
我读过查找并替换多个文件中的特定术语问题以获得指导。我也在使用这个教程,但是我不能实现我的打算去做。所以这里是我的模式。
throw some :: longy :: exception();
我想用这个替换它
抛出CreateException(some :: longy :: exception());
我怎样才能达到这个目的呢?
更新:
此外,如果 some :: longy :: exception()
部分是变体,每个搜索结果的变化?
类似于
throw some :: changing :: text :: exception();
将被转换为
throw CreateException(some :: changing :: text ::你可以使用
sed code $>表达式:
sed's / throw some :: longy :: exception(); / throw CreateException some :: longy :: exception()); / g'
并将它添加到 find
命令检查 .h
, .cpp
和 .hpp
文件(主意来自使用ls和grep添加特定扩展名的文件 ):
find。 '\.\(h \ | cpp \ | hpp\)'
所有在一起:
find。 -iregex'。* \.\(h \ | cpp \ | hpp \)'-exec sed -i.bak's / throw some :: longy :: exception(); / throw CreateException(some :: longy :: exception()); / g'{} \;
请注意 sed -i.bak
以便编辑,但是创建一个 file.bak
备份文件。
变量模式
如果您的模式不同,您可以使用:
sed -r'/ ^ throw / s / throw(。*); $ / throw CreateException(\ 1); /'file
在 throw
开始的行中进行替换。它捕获抛出
到;
之后的所有内容,并将其打印回CreateException();`。
测试
$ cat a.hpp
throw some :: longy ::例外();
扔你();
asdfasdf throw you();
$ sed -r'/ ^ throw / s / throw(。*); $ / throw CreateException(\ 1); /'a.hpp
throw CreateException(some :: longy :: exception ());
throw CreateException(you());
asdfasdf throw you();
I have a pattern that I need to replace in my .hpp
, .h
, .cpp
files in multiple directories.
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
throw some::changing::text::exception();
will be converted to
throw CreateException(some::changing::text::exception());
You can use the sed
expression:
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 . -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.
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();`.
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();
这篇关于查找并替换特定目录中的文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!