Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I seek to a certain position in a file?

by Anonymous Monk
on Apr 27, 2000 at 11:50 UTC ( #9374=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to know how to go to a specific line in a file, followed by the specific char in that line. Could you explain more on the function fseek, and how to utilize it?

Originally posted as a Categorized Question.

  • Comment on How do I seek to a certain position in a file?

Replies are listed 'Best First'.
Re: How do I seek to a certain position in a file?
by btrott (Parson) on Apr 27, 2000 at 22:17 UTC
    Perl doesn't have fseek--it has seek, which does the same thing. As far as I know.

    There really isn't a good way of jumping to a specific line in a file, unless your "lines" are fixed-length. This is because Perl (and your computer in general) thinks of files as a sequence of bytes, not as a sequence of lines.

    There are exceptions (one being files with fixed-length records). Take a look at perlfaq5 and the DB_File manpage (in particular, you'll want to look at the DB_RECNO section).

    One suggestion in perlfaq5 is to build an index of line numbers mapping to byte positions in the file. Then, when you need to jump to a particular line X, you read the byte position for line X from your index, then seek to that byte position.

    This may work for you.

Re: How do I seek to a certain position in a file?
by turnstep (Parson) on Apr 28, 2000 at 01:45 UTC
    If you just want to find a certain line/character, try something like this:
    $line=0; $matchline=5; $char = 1; open(MYFILE, "$myfile") || die; while(<MYFILE>) { ++$line >= $matchline || next; $l=length($_); print "Found line $line (length: $l)\n$_\n"; if ($l>$char) { print "Character $char is ", substr($_,$char-1,1), "\n"; } last; }
Re: How do I seek to a certain position in a file?
by DamnDirtyApe (Curate) on Nov 13, 2002 at 00:52 UTC

    To get to a known row in a text file, take a look at the Tie::File module on CPAN.

Re: How do I seek to a certain position in a file?
by simonflk (Pilgrim) on Nov 13, 2002 at 15:57 UTC
    GhodMode said:
    my @array = <FILE>; close FILE; # Print line 10 print $array[10];

    Not quite. That prints line 11. I would do something like this:

    my $findline = 10; while (<FILE>) { next unless $. == $findline; # .. do something last; #don't read any more }

    For the original question "do something" would probably be a seek backwards to the desired char, or perhaps more sensibly, stop the line before and seek forwards.

    for more info on $. and friends, see perldoc perlvar

    -- simonflk

Re: How do I seek to a certain position in a file?
by GhodMode (Pilgrim) on Nov 12, 2002 at 15:42 UTC
    open (FILE, "filename.txt") or die "Could not open filename.txt"; # Store the contents of the file in an array, one line per element my @array = <FILE>; close FILE; # Print line 10 print $array[10];
      If there is a specific pattern you are looking for then you might want to use a if statement and match for the pattern using the ~m/pattern/ Shashidhar Iddamsetty

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://9374]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2021-04-21 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?