使用bash / Perl的基于每个文件名修改文件 [英] Using Bash/Perl to modify files based on each file's name
问题描述
我有几百个文件,这些文件都包含相同的类,过程的变化。我的目的是让每一个类派生类中一些基本化酶BaseProcess'的。
每个文件已手动重命名为类似Process_XXX',我试图找到通过每一个文件去和不断变化的每一次出现,例如方式:
I have a few hundred files which all contain variations of the same class, 'Process'. My intention is to make each class a derived class of some base clase 'BaseProcess'. Every file has been manually renames to something like 'Process_XXX' and I am trying to find a way of going through every file and changing every occurrence of, for example:
class Process {
stuff;
}
到
class Process_XXX : public BaseProcess {
stuff;
}
其中XXX是从特定文件采取
被改变。这是可能使用bash或Perl脚本?或者是它最好手动进行?
干杯
杰克
where XXX is taken from the particular file being changed. Is this possible using Bash or Perl scripting? Or is it best to proceed manually? Cheers Jack
推荐答案
我将添加一个答案考虑文件扩展名(加上我建议的的回答通过 fedorqui )以及来自的 DVK的 回答:
I'll add an answer taking into account the file extensions (adding the changes I suggested to the answer by fedorqui) as well as improvements from DVK's answer:
for SUFFIX in cc h java txt; do
for file in *.$ext; do
sed -i.bak "s#^\\(\\s*class\\s\\+Process\\)\\(\\s*{\\)#\\1_${file/%.$SUFFIX} : public BaseProcess\2#g" $file
done
done
模式的一个例子是取值#^ \\(\\ S *类\\ S \\ +流程\\)\\(\\ S * {\\)#\\ 1_AlertGenerator:公共BaseProcess \\ 2#摹
与 XXX
是 AlertGenerator
。我使用的是两个 \\(... \\)
来提取匹配,并在替换模式使用它们作为 \\ 1
和 \\ 2
(名为回
在信息的sed $ C $引用C>)。
An example of the pattern is s#^\(\s*class\s\+Process\)\(\s*{\)#\1_AlertGenerator : public BaseProcess\2#g
with the XXX
being AlertGenerator
. I'm using the two \(...\)
to extract the matches and use them in the replacement pattern as \1
and \2
(called back references
in info sed
).
这篇关于使用bash / Perl的基于每个文件名修改文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!