Perl : to print the lines between two patterns

Hello experts,

I have a text file from which I need to print all the lines between the patterns.

Could anyone please help me with the perl script.

names.txt

Badger
Bald Eagle
Bandicoot
Bangle Tiger
Barnacle
Barracuda
Basilisk
Bass
Basset Hound
Beetle
Beluga Whale
Bird
Big-horned sheep
Bird of paradise
Be
Billy goat bandad
indobill
From the above file...

I will be entering any of the 2 names and will be printing all the lines in between the patterns.

$source= "Badger"
$dest= "goat"

I have to all the lines in between Badger and goat.

output should be as below..

Badger
Bald Eagle
Bandicoot
Bangle Tiger
Barnacle
Barracuda
Basilisk
Bass
Basset Hound
Beetle
Beluga Whale
Bird
Big-horned sheep
Bird of paradise
Be
Billy goat

Could anyone please help with the perl script.

Regards,
Giridhar

perl -ne '(/Badger/ .. /goat/) && print' file

You could do this with sed too:

sed -n '/Badger/,/goat/p' file
1 Like

Thanks balajesuri ...

Instead of perl one liner Could you please use the perl script ?

First open the file and loop through the lines. Use the condition provided in the one liner and you can get the result.

if ( /Badger/ .. /goat/ ) {
    print;
}

Let us know what you've attempted.

file 019

AAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBB
top
CCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDD
EEEEEEEEEEEEEEEEEEEE
GGGGGGGGGGGGGGGGGGGG
tail
EEEEEEEEEEEEEEEEEEEE
FFFFFFFFFFFFFFFFFFFF
GGGGGGGGGGGGGGGGGGGG

the one liner Perl works fine :

dpi@loppro01:/home/dpi/patrick/unix.com %perl -ne '(/top/ .. /tail/) && print' file019
top
CCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDD
EEEEEEEEEEEEEEEEEEEE
GGGGGGGGGGGGGGGGGGGG
tail

but the following program do not work :

#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = "$cur_dir/$ARGV[0]";

my $record;

open(FILE,"<$filename") or die"open: $!";
while( defined( $record = <FILE> ) ) {
  chomp $record;
  if ( /top/ .. /tail/ ) {
    print "$record\n";
  }
}
close(FILE);
%./file019.pl file019
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 1.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 2.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 3.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 4.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 5.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 6.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 7.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 8.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 9.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 10.
Use of uninitialized value in pattern match (m//) at ./file019.pl line 12, <FILE> line 11.

Where is my error(s) located ?

Thank You

if ( $record =~ /top/ .. $record =~ /tail/ )
1 Like

Thank you all,

I've never used

..

in Perl.
It seems to be very useful.
Can you tell me more about it, the way it works ?

Fancy name for .. is flipflop operator. Take a look at the perldoc.

1 Like