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

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

apple
banana
orange
pineapple

 

This markup will print lines that contain "apple". The "i" option here is to ignore case (e.g. case insensitive match).

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

while (my $line = <FH>) {
  if ($line =~ /apple/i) {
     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 aa535 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |