Solved

Clearing a flatfile

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now