how do i check a file for a pattern and perform an action if it doesn't exist?
i know how to search a file for a pattern. you just place it in an array like so.
#!/usr/bin/perl
my $data_file = "file.txt";
open DATA, "$data_file";
my @array_of_data = <DATA>;
if ($_ =~ m/pattern/i) {
print "\nfile contains pattern";
}
close DATA
however, if you want to find out if a pattern doesn't exist, the search will always be true because each line is read separately and not all lines in the file contain the pattern.
how do search the whole file for the non-existence of the pattern?
[^something] matches any character except those that [something] denotes; that is, immediately after the leading �[�, the circumflex �^� means �not� applied to all of the rest
[^abc]+ any (nonempty) string which does not contain any of a, b and c (such as defg)
You can search the file for the pattern, and set a variable to a certain value when a match is found. If the variable is not assigned this value, then there is no match. Does that make any sense to you?
i didn't know there was a 'grep' command until i curiously typed into google "perl +grep". i'm very new to perl by the way.
#!/usr/bin/perl
my $data_file = "file.txt";
open CHK_ARRAY, $data_file;
my @chk_array = <CHK_ARRAY>;
close CHK_ARRAY;
if (grep(/pattern/,@chk_array) eq 0) {
print "\npattern not here!";
}