[問題] find in files and replace substring

看板Ruby作者 (true me)時間15年前 (2009/12/03 11:12), 編輯推噓1(106)
留言7則, 2人參與, 最新討論串1/1
環境: 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

12/03 11:49, , 1F
不太懂,你把 __FILE__, __LINE__ 拿掉,不就 compile不過?
12/03 11:49, 1F

12/03 13:55, , 2F
當然%s %d format string也會拿掉 :P
12/03 13:55, 2F

12/03 14:54, , 3F
可否貼一段該 c 程式?__FILE__ 和 %s 之類的應該沒關係
12/03 14:54, 3F

12/03 14:54, , 4F
是 printf 的 debug code?
12/03 14:54, 4F

12/03 14:56, , 5F
Yes, 多謝godfat
12/03 14:56, 5F

12/03 14:57, , 6F
我暫時改用File.foreach, =~, gsub!, File.open("tmpf")
12/03 14:57, 6F

12/03 14:57, , 7F
和FileUtils.mv來答成我的需求
12/03 14:57, 7F
文章代碼(AID): #1B5oomyi (Ruby)
文章代碼(AID): #1B5oomyi (Ruby)