I’ve got a file containing all my sent email for the past decade in date order. I want to find it for the years 2011 and 2012. My bash (or perl?) scripting skills are pretty rusty, but here is the pseudocode for what I need to do:
******************************
Open original text file read only
Open new file for writing
Set Printflag to “false”
For each line of file
If match regular expression “Date: .* 2011” set Printflag to “true.”
If match regular expression “Date: .* 2013” set Printflag to “false.”
If Printflag, copy line to new file
Close both files.
******************************
Note that the regular expression may have to be a little more specific (like maybe include a match for [Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec] between “Date:” and the year). The format for a full line would be
“Date: Fri, 02 Oct 2009 14:41:31 -0400”
The idea is to scan the file until 2011 begins, start echoing lines to the new file, and stop doing it when 2013 begins. Maybe exit at that point so it doesn’t waste time or cycles going through the rest of it.
[Update a while later]
Never mind, I got the answer from Twitter.
[Saturday-morning update]
For those curious, here’s the solution to the problem:
$ perl -ne “print if /Date:.*2011/../Date:.*2013/” $oldfile >> $newfile