![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 03:17 AM |
| Perl: searching for a string in a file... | pondlife | Shell Programming and Scripting | 4 | 09-17-2007 07: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 08:58 PM |
| Help with searching a text file | thekid2 | Shell Programming and Scripting | 6 | 01-02-2004 02:09 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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. |
|
||||
|
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. |
|
||||
|
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.... |
|
|||||
|
Hi.
Changing 4 characters in the regular expression allows: Code:
% ./p3 data1 data2 data3 ----- File contains: United Champions Liverpool Losers Torres Oh, a miss! ----- File contains: United Champions Ronaldo Liverpool Losers Torres Hit! ----- File contains: United Champions Liverpool Losers Torres Ronaldo Oh, a miss! |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| linux, perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|