The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
HELP! PERL script to find matched pattern kimhuat Shell Programming and Scripting 1 05-12-2008 10:24 AM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 09:10 AM
find and replace a pattern in a file krishnamaraju Shell Programming and Scripting 1 08-29-2006 10:02 AM
Find script with input pattern file iguanathompson Shell Programming and Scripting 8 02-06-2006 06:23 PM
how to find the exact pattern from a file? surjyap Shell Programming and Scripting 7 12-05-2005 09:00 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2007
mjays mjays is offline
Registered User
  
 

Join Date: Apr 2007
Location: london
Posts: 40
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 (permalink)  
Old 09-18-2007
SanjayLinux SanjayLinux is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 58
Wink 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 (permalink)  
Old 09-18-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 (permalink)  
Old 09-18-2007
mjays mjays is offline
Registered User
  
 

Join Date: Apr 2007
Location: london
Posts: 40
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 (permalink)  
Old 09-19-2007
mjays mjays is offline
Registered User
  
 

Join Date: Apr 2007
Location: london
Posts: 40
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!";
}
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0