The UNIX and Linux Forums  

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
Extracting a string from one file and searching the same string in other files mohancrr Shell Programming and Scripting 1 09-19-2007 04:17 AM
Perl: searching for a string in a file... pondlife Shell Programming and Scripting 4 09-17-2007 08:35 AM
Hi! Searching for a text string in UNIX skwadim UNIX for Dummies Questions & Answers 1 12-22-2006 01:13 PM
Searching directory for file that contains some text. LordJezo UNIX for Dummies Questions & Answers 4 04-04-2006 09:58 PM
Help with searching a text file thekid2 Shell Programming and Scripting 6 01-02-2004 02:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2008
meevagh meevagh is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 7
PERL: Searching for a string in a text file problem

Looking for a bit of help. I need to search for a string of words, but unfortunately these words are located on separate lines.

for example the text output is:

United
Chanmpions
Ronaldo
Liverpool
Losers
Torres


and my script code is

print("DEBUG - checking file message");
while (<FILE>){
$line = $_;

if($line =~ /United/ ){
print("\nAbout to send email\n");
sendEmail($contacts,
"",
"Monitoring",
"\nPlease be aware that there is a problem.",
"",
"");
}

the above script will send out an e-mail when it locates United, but I need to send out an e-mail when it gets United Champions Ronaldo.

I thought something like
if($line =~ /United/n Champions/n Ronaldo/)

But no luck.

Any suggestions as to how to go about this.
  #2 (permalink)  
Old 04-28-2008
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
$line =~ /(United|Chanmpions|Ronaldo|LiverpoolLosers|Torres)/i
  #3 (permalink)  
Old 04-28-2008
meevagh meevagh is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 7
Hi Photon, Thanks for the reply.
But not quite what I am trying to do, I prob could have explained it a bit better.

My problem is that I only want to send out an e-mail if the lines
United
Champions
Ronaldo
occur directly after each other as in.

sample text file

United
Chanmpions
Ronaldo
Liverpool
Losers
Torres


I dont want to send an e-mail just if the words are located in the file for example I dont want to send an e-mail if text file is

United
Champions
Torres
Liverpool
Losers
Ronaldo

as the lines im interested dont occur in the correct order.
  #4 (permalink)  
Old 04-28-2008
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
$line =~ /[United]?\s?[Chanmpions]?\s?[Ronaldo]?\s?/

Last edited by photon; 04-28-2008 at 03:43 PM..
  #5 (permalink)  
Old 04-28-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by photon View Post
$line =~ /[United\s]?[Chanmpions\s]?[Ronaldo\s]?/
The above code is wrong as it is an incorrect use of character classes: [] which match any of the characters inside the square brackets in any order. You can't use them to match whole words, at least not easily or effciently or in the way they are used above. Plus that would also check just one line of the file at a time.
  #6 (permalink)  
Old 04-28-2008
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
I changed it, point was to use one liner regular expression instead of using hashes and loops and getting all complex. I wish I had the time to test all my code but I am on work time.
  #7 (permalink)  
Old 04-28-2008
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
Two approaches....

1. Search for any of the words on each line and every time you find one, add it to a hash... e.g. $somehash{"Renaldo"} = 1;

When you've finished scanning the whole file, check the resulting hash for the existence of all the words....

if (exists $somehash{"Renaldo"} && exists $somehash{"loosers"} && exists ... ) { send email ... }

Something like that....

OR....

You could try a pattern like

$FILEBUFFER =~ /(A|B|C|D).+(A|B|C|D).+(A|B|C|D).... /is

You simply repeat the alternatives over and over again separated by one or more of any character, and that way you catch all of them if present no matter what the order.... You have to test the resulting capture to see if all words are present... Note the "is" at the end of the pattern... "i" causes case to be ignored, and "s" says to count a newline as one of the "any characters" which lets you match across lines... Note that in this case $FILEBUFFER contains the WHOLE file (see READ()), not a line....
Closed Thread

Bookmarks

Tags
linux, perl, perl shift, shift, shift perl

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 03:39 PM.


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