递归遍历目录并替换函数调用
我想递归地遍历一个目录,并找到至少具有以下一组函数调用之一的所有文件:
A(a)
B(a,b)
C(a,b,c)
现在,忽略参数,我可以获得此类文件的列表,
grep -r -l '[A-C](' .
尽管我确信我也可以匹配参数不知何故。在这些文件上,我想要执行以下操作:首先,我想要一个备份,即将原始文件保存到 filename.ext_bak 或其他文件中,而在 filename.ext 中,我想将函数调用的每次出现替换
X(a,...)
为
#ifdef LOL
X_new(f(a),...)
#else
X(a,...)
#endif
X 可以是A、B、C 并注意 X_new 中的每个参数都包装在函数 f(...) 中。 将不胜感激任何帮助!提前致谢!
I would like to traverse a directory recursively and find all files that have at least one of the function calls of the following set:
A(a)
B(a,b)
C(a,b,c)
now, disregarding the arguments I can get a list of such files with
grep -r -l '[A-C](' .
although I am sure I can also match the arguments somehow. On these files I want to do the following: First, I want a backup, i.e. save the original file to filename.ext_bak or something, whereas in the filename.ext I want to replace each occurence of the function call
X(a,...)
by
#ifdef LOL
X_new(f(a),...)
#else
X(a,...)
#endif
where X can be A,B,C and notice that each argument in X_new is wrapped in a function f(...).
Would appreciate any help! Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里使用 os.walk 递归地遍历所有文件(从当前文件开始)工作目录)。
backup='_bak'
参数告诉 fileinput.input< /a> 备份每个文件。This uses os.walk to traverse all files recursively (starting from the current working directory).
The
backup='_bak'
argument tells fileinput.input to make a backup of each file.做出很多假设。如果其中之一产生太多问题,请告诉我。
Makes a lot of assumptions. Let me know if one of them produces too many problems.