Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > find lines in txt file grouped on date string and delete that group each day

Reply
Thread Tools

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

 
 
joeyej
Guest
Posts: n/a
 
      04-11-2006
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

 
Reply With Quote
 
 
 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      04-11-2006
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

 
Reply With Quote
 
 
 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      04-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
joeyej
Guest
Posts: n/a
 
      04-11-2006
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

(E-Mail Removed) wrote:
> (E-Mail Removed) 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


 
Reply With Quote
 
joeyej
Guest
Posts: n/a
 
      04-11-2006
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 <(E-Mail Removed). com>,
> joeyej <(E-Mail Removed)> 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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-12-2006
joeyej <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Now.day and day(now) returning the wrong day! Andy ASP .Net 1 11-06-2008 12:20 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
How to display a string in many lines, each lines have a specified length thuyptt@dsp.com.vn C++ 1 12-06-2005 07:26 AM



Advertisments