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

Discussion in 'Perl Misc' started by joeyej, Apr 11, 2006.

  1. joeyej

    joeyej Guest

    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
    joeyej, Apr 11, 2006
    #1
    1. Advertising

  2. joeyej

    Guest

    joeyej wrote:
    > 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
    , Apr 11, 2006
    #2
    1. Advertising

  3. joeyej

    Guest

    wrote:
    >
    > 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
    , Apr 11, 2006
    #3
  4. joeyej

    joeyej Guest

    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

    wrote:
    > wrote:
    > >
    > > 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
    joeyej, Apr 11, 2006
    #4
  5. joeyej

    joeyej Guest

    Thanks Jim -

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

    Joe

    Jim Gibson wrote:
    > In article <>,
    > joeyej <> wrote:
    >
    > > 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!
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
    joeyej, Apr 12, 2006
    #5
  6. joeyej <> wrote:
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 12, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    588
    Matthew Speed
    Nov 8, 2003
  2. Matt
    Replies:
    2
    Views:
    505
    Pete Becker
    Nov 8, 2003
  3. Replies:
    1
    Views:
    439
    Jonathan Mcdougall
    Dec 6, 2005
  4. Murali
    Replies:
    2
    Views:
    544
    Jerry Coffin
    Mar 9, 2006
  5. Andy
    Replies:
    1
    Views:
    685
Loading...

Share This Page