http://www.perlmonks.org?node_id=9374

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
A reply falls below the community's threshold of quality. You may see it by logging in.