作者Knudsen (true me)
看板Ruby
标题[问题] find in files and replace substring
时间Thu Dec 3 11:12:45 2009
环境:
e.g. Ruby 1.9.1p243
状况:
e.g. 我想要在一堆C files里面去除printf中有用到__FILE__和__LINE__的地方
Hi all,
我有一堆C files, 里面有很多hardcode写的__FILE_和__LINE__, 我不喜欢这种coding
sytle, 所以想把全部用到的地方拿掉, 所以想写个script做这件事情, 对於Ruby和
Regular expression不太熟, Google也Goo不太到,
现在我写了
Find.find(FOLDER) do |f|
if File.fnmatch?("**.c", f) && File.file?(f)
File.open(f) do |ff|
ff.grep( /(%s : %d)[a-z|A-Z|\w|\s|\W|\S]*(__FILE__,[\s]*__LINE__[\s]*[,]*)/ ) do |line|
puts "#$1, #$2"
#希望在这里可以把有问题的line把$1和$2用空白取代掉再写回file
end
end
end
我知道code看起来怪怪的啦,可以帮我解答吗?
Thanks.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.209.21
1F:推 godfat:不太懂,你把 __FILE__, __LINE__ 拿掉,不就 compile不过? 12/03 11:49
2F:→ Knudsen:当然%s %d format string也会拿掉 :P 12/03 13:55
3F:→ godfat:可否贴一段该 c 程式?__FILE__ 和 %s 之类的应该没关系 12/03 14:54
4F:→ godfat:是 printf 的 debug code? 12/03 14:54
5F:→ Knudsen:Yes, 多谢godfat 12/03 14:56
6F:→ Knudsen:我暂时改用File.foreach, =~, gsub!, File.open("tmpf") 12/03 14:57
7F:→ Knudsen:和FileUtils.mv来答成我的需求 12/03 14:57