[Note: parts of this message were removed to make it a legal post.]
First, let's create a rough, high-level solution to your problem.
read the last line of a file
if the last line matches a pattern
process only the last line
else
process the file starting at the first line
end
If that's accurate, the next step that I'd take is figuring out ways to read
the last line of the file. You want to use FasterCSV. If I go to the
FasterCSV site [
http://fastercsv.rubyforge.org/ ] and click on the class
FasterCSV, I see that I can read all lines from a file like this:
arr_of_arrs = FasterCSV.read("path/to/file.csv")
If the file isn't large, then reading it all into memory is probably fine.
If it is large, you should adapt what follows to use a solution like James
proposed where you read a line, process it and write it instead of reading
everything into memory.
Now that I have the whole file in an array of arrays (
[[field1,field2,field3],[field1,field2,field3]] ), I can just ask for the
last line, so to speak:
last_line = arr_of_arrs.last
and check it for the match:
# I'm making an assumption about the structure of the last line being
something like
# XYZ,some content,some more content,end of line
# which would've been parsed into an array
# ["XYZ","some content","some more content","end of line"]
# and you just wanting to check the first field on the last line
if last_line.first == "XYZ"
do something based on the contents of last_line
else
# since we have the whole file parsed into an array of arrays by
FasterCSV,
# just process the whole thing from the beginning
arr_of_arrs.each do |line|
# make some changes to content
# write changed line to new csv file
end
end
From this point, hopefully you can fill in what needs to be filled in. Don't
hesitate to write back if we can help further.
Regards,
Craig