Solved

Clearing a flatfile

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

19 Experts available now in Live!

Get 1:1 Help Now