find lines in txt file grouped on date string and delete that group each day

J

joeyej

Hi - I have a txt file I'd like lines removed from grouped on date. I
want to run a daily task at midnight to read the dates.inc file, locate
the [same] group dates at the top and delete them i.e read the file,
find "May 15" or "May 15, 2006," and remove the lines containing that
string stopping at "May 16,". The program would kick off at night to
strip one date grouping per run.

<option value="May 15, 2006, (Monday), 10am">May 15, 2006, (Monday),
10am
<option value="May 15, 2006, (Monday), 1pm">May 15, 2006, (Monday), 1pm
<option value="May 16, 2006, (Tuesday), 9am">May 16, 2006, (Tuesday),
9am
<option value="May 16, 2006, (Tuesday), 11am">May 16, 2006, (Tuesday),
11am
<option value="May 16, 2006, (Tuesday), 1pm">May 16, 2006, (Tuesday),
1pm
<option value="May 17, 2006, (Wednesday), 9am">May 17, 2006,
(Wednesday), 9am
<option value="May 17, 2006, (Wednesday), 11am">May 17, 2006,
(Wednesday), 11am
<option value="May 17, 2006, (Wednesday), 1pm">May 17, 2006,
(Wednesday), 1pm
<option value="May 17, 2006, (Wednesday), 3pm">May 17, 2006,
(Wednesday), 3pm

Thanks,

Joe
 
J

jl_post

joeyej said:
Hi - I have a txt file I'd like lines removed from grouped on date. I
want to run a daily task at midnight to read the dates.inc file, locate
the [same] group dates at the top and delete them i.e read the file,
find "May 15" or "May 15, 2006," and remove the lines containing that
string stopping at "May 16,". The program would kick off at night to
strip one date grouping per run.

<option value="May 15, 2006, (Monday), 10am">May 15, 2006, (Monday),
10am
<option value="May 15, 2006, (Monday), 1pm">May 15, 2006, (Monday), 1pm
<option value="May 16, 2006, (Tuesday), 9am">May 16, 2006, (Tuesday),


Dear Joe,

So you want to delete lines from (and including) the first mention
of "May 15" to (but not including) the first mention of "May 16"?

If I'm correct, you can easily do so with this one-liner:

perl -ne "print unless /May 15/ .. /May 16/ and not /May 16/" file.txt

This will remove any lines from file "file.txt" that are on or after
"May 15" but not on or after "May 16" (unless a "May 15" line is
encountered again).

I hope this helps, Joe.

-- Jean-Luc
 
J

jl_post

perl -ne "print unless /May 15/ .. /May 16/ and not /May 16/" file.txt


Sorry, I forgot the '-i' switch (for "in place" editing). What I
meant to say was:

perl -ni -e "print unless /May 15/ .. /May 16/ and not /May 16/"
file.txt

-- Jean-Luc
 
J

joeyej

Hi Jean-Luc -

Thanks but the idea is not to have specific date search criteria coded
in since the file includes many date groupings. I was hoping for code
that would read the date grouping at the beginning of the dates.inc
file and delete only the lines containing that same string. This
process would run each midnight until all date groups were [singly]
removed.

Joe
 
J

joeyej

Thanks Jim -

You are correct in your assumption that I'm dealing with only one file.
I'll try your suggestion.

Joe

Jim said:
joeyej said:
Hi - I have a txt file I'd like lines removed from grouped on date. I
want to run a daily task at midnight to read the dates.inc file, locate
the [same] group dates at the top and delete them i.e read the file,
find "May 15" or "May 15, 2006," and remove the lines containing that
string stopping at "May 16,". The program would kick off at night to
strip one date grouping per run.

<option value="May 15, 2006, (Monday), 10am">May 15, 2006, (Monday),
10am
<option value="May 15, 2006, (Monday), 1pm">May 15, 2006, (Monday), 1pm
<option value="May 16, 2006, (Tuesday), 9am">May 16, 2006, (Tuesday),
9am
<option value="May 16, 2006, (Tuesday), 11am">May 16, 2006, (Tuesday),
11am
<option value="May 16, 2006, (Tuesday), 1pm">May 16, 2006, (Tuesday),
[...]

It is not clear from your description whether you are dealing with one
file or two. Assuming there is only one file and you are attempting to
remove the lines from the beginning of the file that all have the same
date (does the date have any relation to the day on which the program
is run?), you will want to do something like (all examples untested):

1. Open the old file:
open( my $old, '<', 'date.inc' ) or die( ...

2. Open a new file
open( my $new, '>', 'date.new' ) or die( ...

3. Read the first line and extract the date. If your lines really are
all with the same format as shown above, you can use the substr
function:

my $first_line = <$old>;
my $date = substr($first_line,15,12);

4. Read the rest of the lines and skip the lines with the same date
string:

while( <$old> ) {
print $new if index($_,$date) == -1;
}
close($old) or die( ...
close($new) or die( ...


5. Rename the new file to the old file (and rename the old file to a
backup name if desired).

rename( 'date.inc', 'date.bak' ) or die( ...
rename( 'date.new', 'date.inc' ) or die( ...

If your problem is more complicated than that, please explain further.
However, you are more likely to get help here if you make an attempt at
solving your problem by yourself and come here with questions about why
your program is not doing what you want it to do. And please read the
guidelines for this newsgroup so you can help other people help you.

Thanks. Good luck!
 
T

Tad McClellan

joeyej said:
Hi - I have a txt file I'd like lines removed from grouped on date. I
want to run a daily task at midnight to read the dates.inc file, locate
the [same] group dates at the top and delete them i.e read the file,
find "May 15" or "May 15, 2006," and remove the lines containing that
string stopping at "May 16,". The program would kick off at night to
strip one date grouping per run.


OK.

What part of all of that are you stuck on?

If you show us the code you have so far, we will help you fix it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top