?
Solved

Flushing text file of # of records

Posted on 2003-04-01
2
Medium Priority
?
220 Views
Last Modified: 2010-03-05
I have a text file on the server which X number of records. I would only like to keep the first 600 records and delete the rest. The file is set up one record per line.  How do I open, read, and flush the records in that file.  Preferably at the time when a new record is submitted.
0
Comment
Question by:acdesigns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 100 total points
ID: 8248279
Since this is on the perl board, I'll answer in perl.  These should all do what you want.  All of these assume the filename (including path if not in current directory) is in $file.

# simplest, but does a system which is inefficient
system "mv $file /tmp/trunc.$$; head -600 /tmp/trunc.$$ > $file; rm /tmp/trunc.$$";

use File::Copy qw(mv);
mv $file, "/tmp/trunc.$$" or die $!;
open IN, "/tmp/trunc.$$" or die $!;
open OUT, ">$file" or die $!;
while (<IN>) {
     print OUT;
     last if ($. == 600);
}
close OUT;
close IN;

use Fcntl qw(O_RDWR O_EXLOCK);
use Tie::File;
# open file for reading and writing with an exclusive lock
tie @file, 'Tie::File', $file, mode => O_RDWR|O_EXLOCK or die $!;
# remove lines after 600
splice @file, 600;
untie @file;
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8253250
So, are you adding records at the beginning of the file? It makes a difference whether you want to chop lines off the end of the file or off the front.

Wilcoxon's Tie::File solution can be adapated to either, and can be used for inserting the record as well. You just need to make sure you're not throwing away the most recently added record when you trim the file.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question