Bootstrap FreeKB - Perl (Scripting) - Previous, current, and next line in a file (seek, tell)
Perl (Scripting) - Previous, current, and next line in a file (seek, tell)

Updated:   |  Perl (Scripting) articles

Let's say you have a file (fruit.txt) that contains fruit. The seek and tell operators can be used to get the previous and next lines in a file.

apple
pineapple
orange
grapes

 


TELL

The tell operator simply determine the current position in a file. 

#!/usr/bin/perl

use strict;
use warnings;

my $file = "fruit.txt";

open (FH, "<", $file) or die "cannot open $file $! \n";

while (my $line = <FH>) {
  my $current_position = tell FH;
  print "$current_position $line\n";
}

close FH;

 

This will produce the following results. While in the first loop, the cursor was at position 6 (the new line following the word apple).

6  apple
16 pineapple
23 orange
30 grapes

 


SEEK

The seek operator is used to go to a certain position in the file. For example, the following would move you to 0:0, which is the very first character in the file, which is the letter "a" in apple in this example. The first 0 means "byte 0" and the second 0 means "from the beginning of the file". Or, you could use SEEK_SET instead of 0 to start at the beginning of the file. 

seek FH, 0, 0;
seek FH, 0, SEEK_SET;

 

Or to move to position 7 (the first letter "p" in pineapple). The 7 means "byte 7" and the 0 means "from the beginning of the file". Or, another way to describe this is "starting at the beginning of the file, move to the 7th byte in the file".

seek FH, 7, 0;

 

This script will loop through each line in fruit.txt and will print the previous line, current line, and next line.

#!/usr/bin/perl

use strict;
use warnings;

my $file = "fruit.txt";

open (FH, "<", $file) or die "cannot open $file $! \n";

my $starting_current_position = tell FH;
my $ending_current_position  = tell FH;
my $previous_position        = undef;
my $previous_line            = undef;

while (my $current_line = <FH>) {

  chomp $current_line;

  $starting_current_position = tell FH;

  print "previous position          = $previous_position\n";
  print "starting current position  = $starting_current_position\n";
  print "ending current position    = $ending_current_position\n";

  if (defined $previous_position) {
    seek FH, $previous_position, 0;
    $previous_line = <FH>;
    chomp $previous_line;
    seek FH, $starting_current_position, 0;
  }

  my $next_line = <FH>;
  chomp $next_line;
  seek FH, $starting_current_position, 0;

  print "previous line = $previous_line\n";
  print "current line  = $current_line\n";
  print "next line     = $next_line\n";

  $previous_position = $ending_current_position;
  $ending_current_position = tell FH;
}

close FH;

 

Running this script will produce the following output.

previous line = 
current line  = apple
next line     = pineapple

previous line = apple
current line  = pineapple
next line     = orange

previous line = pineapple
current line  = orange
next line     = grapes

previous line = orange
current line  = grapes
next line     = 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8db87f in the box below so that we can be sure you are a human.