Skip to content

Instantly share code, notes, and snippets.

@chankeypathak
Last active August 29, 2015 14:03
Show Gist options
  • Save chankeypathak/fa7b88f7a100368a49c8 to your computer and use it in GitHub Desktop.
Save chankeypathak/fa7b88f7a100368a49c8 to your computer and use it in GitHub Desktop.
Get line number of matched pattern in Perl
#!/usr/bin/perl
use strict;
use warnings;
my $data;
{local $/=undef; $data = <DATA>};
if ($data =~ /MATCH_ME/){
print "Found MATCH_ME at Line number: ";
my ($line_num) = (substr $data, 0, $-[0]) =~ s{$/}{}g + 1;
print "$line_num";
}
__DATA__
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. MATCH_ME It was popularised in the 1960s with the release of
Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
software like Aldus PageMaker including versions of Lorem Ipsum.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment