FreeKB - Determine if file contains a string of data in Perl
Determine if file contains a string of data in Perl

Home > Search


Let's say you have a file that contains fruit.

apple
banana
orange
pineapple

 

This markup will print lines that contain "apple".

my $file = "/path/to/file.txt";
open(FH, "<", $file) or die "cannot open $file $! \n";

while (my $line = <FH>) {
  if ($line =~ "apple") {
     print $line;
  }
}

close FH;

 

In this example, apple and pineapple will be printed.

apple
pineapple

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments