Solved

Clearing a flatfile

Posted on 2003-10-21
2
124 Views
Last Modified: 2013-12-25
Hi:

I'm a novice with progamming, and I need urgently a simple Perl script code to put in cron to do a clear task, this way:

I have a flat file, where there is a field which is a sequence number. Each record has this field added by one, as the records were being created. Something like this:

aaaa|aaaa|0001
bbbb|bbbb|0002
cccc|cccc|0003
.
.
xyz|xyz|1000


All I need is to clear this file, deleting records, leaving only the "n" most recent, that is, the "n" records (lets say 50) with the greatest values in this field.

Thanks a lot for this great  help.

Mario Lima./
0
Comment
Question by:multisites
  • 2
2 Comments
 
LVL 8

Expert Comment

by:inq123
ID: 9595001
Hi multisites,

name the following code tmp.pl and launch it with "perl tmp.pl myfile >newfile" (myfile contains your data and newfile contains the lattest n entries with biggest numbers):

$n = 50;
while(<>)
{
  $lines{$1} = $_ if(/(\d+)$/);
}
foreach (sort { $b <=> $a } keys %lines)
{
  print $lines{$_};
  last if ++$count >= $n;
}

Cheers!
0
 
LVL 8

Accepted Solution

by:
inq123 earned 250 total points
ID: 9638208
multisites,

It's been a week already, got any result yet?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

821 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