Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

going to a particular Line Number in a File ??

Status
Not open for further replies.

ryen

Programmer
Jul 29, 2002
22
US
I'm aware of PERL's seek function, but it only accepts the byte-position, correct? Is there any way to go to a particular line number in a file? That is, without making a custom function which reads each newline character, and keeps track of the line number, etc.

For instance, say I wanted to go to line 50,045.. i don't see any way it would could be built into perl, but it'd be nice if it was, any help would be greatly appreciated.
-Ryen
 
open(&quot;<$file&quot;,DATA) || die &quot;dead $!&quot;;
while (<DATA>) {
print &quot;\n$_[$wanted]&quot;;
}
close(DATA);

not sure about this last one u could also do :

open(&quot;<$file&quot;,DATA) || die &quot;dead $!&quot;;
@storer;
close(DATA);
print &quot;\n$storer[$wanted]&quot;; someone knowledge ends where
someone else knowledge starts
 
hmm... I think I see what you're going for, I'll experiment and see if I can figure anything out. Thanks a bunch for the input
 
ohhhh sorry i see what you're saying. well, i would do that, but then I'd have to read the whole file into an array... which would work, but not on a really huge file. I think my best bet is to try and do something with the seek command.. which might be hard to do if the database fields/records are varying in size.. and i don't know a lot about binary reading/writing... hmm.. thanks again tho.
 
open(&quot;<$file&quot;,DATA) || die &quot;dead $!&quot;;
while (<DATA>) {
print &quot;\n$_[$wanted]&quot;;
}
close(DATA);


is not working ? someone knowledge ends where
someone else knowledge starts
 
why didn't u say sooner that its was a binary file
read this

thread219-263651 someone knowledge ends where
someone else knowledge starts
 
cause binary file as only one line...
use binmode someone knowledge ends where
someone else knowledge starts
 
you could try

open(F,'file');
while(<F>){
last if $. == 50045;
}
print;

This does go through all the lines in the file until it gets to the right one, but it's quite fast. At least as fast as using the head and tail unix trick.
Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
as said before it is a binary file !

open(&quot;<$file&quot;,DATA) || die &quot;\ndead $!&quot;;
binmode(DATA);
$all_data_is_one_line = <DATA>;
close(DATA); someone knowledge ends where
someone else knowledge starts
 
Oops, I'm sorry I didn't mean it was a binary file.. I just though I should make it a binary file and use the seek function to find a particular piece of data, but I need to learn more about writing binary files first. Sorry about the confusion, I didn't describe that very well.

but Mike, I'll definitely try that, it looks like the fastest solution so far, thanks a bunch.
 
you might like to look at some database solutions, that will give you much faster access to each record

DBM files are good for smallish records that have a good unique key

Otherwise, MySQL is a good starting point for databases.

If you're on a Win PC, then Access files are quick to read using Perl as well. (not that I want to push you down *that* route) Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top