Find selection in current file

Instructional HowTos, posted by users. No questions here please.

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
lotus298
Posts: 6
Joined: Fri Jun 01, 2007 1:20 am

Find selection in current file

Post by lotus298 »

This perl script will search the current file for the selected text.

This works great with version 5's auto-hide command output mode.

Code: Select all

# DOS command should be:
#    perl "c:\userdata\temp\textpad\findinfile.pl" "$File" "$Sel"
# Regex should be:
#    ^([0-9]+):
#    Line is \1

my $filename = $ARGV[0];
my $selection = $ARGV[1];

open (INFILE, "<$filename") || die "Failed to open $filename";
my @lines = (<INFILE>);
close INFILE;

if ($selection eq "") {
  print "Selection is empty.\n";
  exit 0;
} else {
  print "$filename\n";
  print "Searching for \"$selection\"\n";
  print "\n";
}

my $linenum = 1;
foreach $line (@lines) {
  chomp $line;
  if ( $line =~ /${selection}/gi ) {
    my $tmp = sprintf( "%03u", $linenum );
    print "$tmp: $line\n";
  }
  $linenum = $linenum + 1;
}

Post Reply