Topic: Parsing data: Part 2
So, here I am again. I'm a bit more prepared than I was last time, but I'm now getting into the realm of math and stuff.
The task this time is similar to the last one, except I want to take the raw cron job output and parse it down to find out how many times said cron job would execute in a year.
Based on what I learned last time... I started with this.
#!/usr/bin/ruby
def rdcron(line)
(minutes, hours) = (line.scan(/^\S+ \S+/))[0] # Need to complete regex... not sure what to add though.
if hours =~ /(.*)\/(\d+)/
num=$1; dem=$2;
num=1 if num == "\*"
hours = Rational(num,dem).to_f
end
file = File.new("file.log", "r")
while (line = file.gets)
parts = line.split(' ')
puts "#{rdcron(parts[0])} #{rdcron(parts[1])} #{rdcron(parts[2])}"
end
file.close
endIt's not done, there's parts missing, and I have no idea how to progress or even how much of that is right.
A friend of mine gave me a much cleaner beginning:
#!/usr/bin/ruby
def parse_cron(line)
cron = line.scan(/([-0-9*,\/]+)\s([-0-9*,\/]+)\s([-0-9*,\/]+)\s([-0-9*,\/]+)\s([-0-9*,\/]+)\s(.*)/)[0]
{ :min => cron[0], :hour => cron[1], :day => cron[2], :month => cron[3], :week => cron[4], :command => cron[5] }
end
crons = `crontab -l`.split("\n")
# Parses the cron
crons.each do |cron|
p parse_cron(cron)
endBut, I want to fix up the problems in my script and figure out how to make THAT do what I want, and then go from there.
I know what I want to do, but I'm not sure how to do it. Any help would be appreciated.
Edit:
Would num_string or DateTime be viable to accomplish this?
Last edited by Striketh (2011-11-15 18:41:06)