Solved

Clearing a flatfile

Posted on 2003-10-21
2
123 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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 …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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