This webpage contains tutorials on the nuts and bolts of Unix programming(I am guessing a lot of the code here will run with very little modification due to the Mac’s Darwin foundation).
Using Ruby to skim log files
April 5, 2007 · Leave a Comment
Say we have a bunch of log files, whose last line is the main thing you care about(you might imagine it has a distinct prefix like “ANSWER”–I know, that’s contrived :p The following code assumes the script is in the same directory as the log files. It gets all the files ending in .txt, opens them, puts the name of the file to the console, and then puts the result line on the console. We could keep appending to a file, but for a “throwaway” script, we can just make do with: ./process.rb > results.txt(OS X, assuming chmod +x process.rb was applied), or ruby results.txt > process.rb (on Windows).
#!/usr/bin/env ruby
file_list = Array.new
file_list = Dir["*.txt"]
file_list.each do |file|
File.open(file,”r“) do|f_object|
print(file.to_s + “ “)
f_object.each_line {|line| puts line if line =~ /ANSWER/ }
end
end
Categories: Cross-platform code · Regular Expressions · Ruby
Now on Google
April 5, 2007 · Leave a Comment
It seems that you can get to this blog from Google (the RSS feed anyway). I tried “Mac OS X coding Chinmoy Gavini” and this is the only hit. I don’t think you can search the blog from Google…but that’s what the search field on the blog is for!
Categories: Google