perl - how do i find out if a file doesn't contain a pattern?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
perl - how do i find out if a file doesn't contain a pattern?

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.
Code:
#!/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?

hope i've made sense?
# 2  
Hammer & Screwdriver Read this one

Perl regular expressions

[^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)

~~~Sanjay~~~
# 3  
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?
# 4  
thanks guys.

i can use either of those ideas but cbkihong's will be easier to read.

bit annoyed that i didn't think of that myself. i've written loads of unix shell scripts using the 'set variable' method.
# 5  
i've figured out a better solution.

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.

Code:
#!/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!";
}

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #604
Difficulty: Medium
In MySQL 8.0 table encryption can be managed globally by defining and enforcing encryption defaults
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

3. Shell Programming and Scripting

Find file of particular pattern

Hi All, I have a file PSU_ 20130805_201308041234522 i want to search this file where variable day=20130805 and curday=20130804 after currday date some numbers will be added.how to search this file by using day and curday. Thanks in advance. (3 Replies)
Discussion started by: pracheth
3 Replies

4. Shell Programming and Scripting

Why regex pattern doesn't work in find?

$ find /opt/data_* -maxdepth 3 -type d -name "main*" 2> /dev/null /opt/data_025/maindblogs /opt/data_026/maindblogs /opt/data_027/maindblogs /opt/data_028/maindblogs $ find /opt/data_* -maxdepth 3 -type d -name "rvlogs*" 2> /dev/null /opt/data_002/prod/rvlogs2_archive... (4 Replies)
Discussion started by: urello
4 Replies

5. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

6. UNIX for Dummies Questions & Answers

PERL pattern matching in a file

Hi Gurus, I have a file like below.. I have to match each with predefined pattern. If matches then have to write the entire record to a separate file. If not make the value as NULL and write the entire record into another file. | is the delimiter ravi123|2344|M R123Vi|2345|F... (8 Replies)
Discussion started by: pvksandeep
8 Replies

7. Shell Programming and Scripting

To find a pattern in file

Hi, I would like to find a pattern in a file as follows: I would like to find "or" "OR" "and" "AND" between two numeric values. I have tried this: grep '**or*' But does not work. Appreciate help on this. (4 Replies)
Discussion started by: pinnacle
4 Replies

8. Shell Programming and Scripting

How to find this pattern in a file

hi all i have a file in my box, which is so huge and full file is in a single line. In this file i have to look for a pattern "ABC01234567" In this above mentioned pattern ABC is fixed and number might change . it will a eight digit random number Thanks so much for all you help ... (1 Reply)
Discussion started by: Prateek007
1 Replies

9. Shell Programming and Scripting

HELP! PERL script to find matched pattern

Hi all, I just learnt Perl and I encountered a problem in my current project. For a verilog file, i am required to write a PERL script that could match pattern to output nitrolink and nitropack. I wont know what name to grep except the pattern below. the verilog file: nitrolink nitrolink... (1 Reply)
Discussion started by: kimhuat
1 Replies

Featured Tech Videos