Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Clearing a flatfile

Posted on 2003-10-21
2
Medium Priority
?
127 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
[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
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 750 total points
ID: 9638208
multisites,

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

721 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