PERL: Searching for a string in a text file problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: Searching for a string in a text file problem
# 1  
Old 04-28-2008
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  
Old 04-28-2008
$line =~ /(United|Chanmpions|Ronaldo|LiverpoolLosers|Torres)/i
# 3  
Old 04-28-2008
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  
Old 04-28-2008
$line =~ /[United]?\s?[Chanmpions]?\s?[Ronaldo]?\s?/

Last edited by photon; 04-28-2008 at 03:43 PM..
# 5  
Old 04-28-2008
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....
# 6  
Old 04-28-2008
Hi.

Here is a quickly-written possibility:
Code:
#!/usr/bin/perl

# @(#) p2       Demonstrate matching across line boundaries.

use warnings;
use strict;

my ($debug);
$debug = 0;
$debug = 1;

my $file;

for $file (@ARGV) {
  print "\n -----\n";
  my $lines = slurp($file);
  print " File contains:\n$lines";

  print "\n";
  if ( $lines =~ /United.*Champions.*Ronaldo/xms ) {
    print " Hit!\n";
  }
  else {
    print " Oh, a miss!\n";
  }
}

sub slurp {

  # Best practices, p213 for a file.
  my ($file) = shift;
  my ($f);
  open( $f, "<", $file ) || die " Can't open file $file, quitting.\n";
  my $scalar = do { local $/; <$f> };
  return $scalar;
}

exit(0);

Producing output for a bad dataset and a good dataset:
Code:
% ./p2 data1 data2

 -----
 File contains:
United
Champions
Liverpool
Losers
Torres

 Oh, a miss!

 -----
 File contains:
United
Champions
Ronaldo
Liverpool
Losers
Torres

 Hit!

See perl documentation for details ... cheers, drl
# 7  
Old 04-28-2008
Hi.
Quote:
Originally Posted by meevagh
... I prob could have explained it a bit better. ...
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!

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

2. Shell Programming and Scripting

Need comand or script for append text after searching for the desired string

Hi all, i have a generated report in unix in the following command like input.txt 47.85,10 0124,42.35,8 0125,3.5,2 the input file format is fixed I need the my output file with append text as below output.txt 0124 amount:42.35 0125 amount:3.5 0124 count : 8 0125... (34 Replies)
Discussion started by: hemanthsaikumar
34 Replies

3. Shell Programming and Scripting

Searching a string in a file using perl

Hi I would like to read a file using perl and search for a string (last entry). Then read that into an array and do further grep File content for ex: comp=a,value=30,runtime=12,type=lic comp=d,value=15,runtime=2,type=lic comp=a,value=90,runtime=43,type=lic... (1 Reply)
Discussion started by: vivek_damodaran
1 Replies

4. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

5. Shell Programming and Scripting

Searching for a particular string and modifying text within block of data

Hi Forum. Is there a quick way to do the following search/replace within a block of data? I tried to google the solution but didn't really know what to look for. I have the following text file (I want to search for a particular string "s_m_f_acct_txn_daily_a1" and replace the... (5 Replies)
Discussion started by: pchang
5 Replies

6. Shell Programming and Scripting

searching a text string for n'th :

hello, i'm a novice on bsh scripting so thanks for any help here basically i have a shell var $x that looks like this > echo $x nabc1234:!:73394:17155:Gary Mason:/home/garym:/bin/ksh and i'm trying to keep the first 8 characters and the text from the 4th : to the 5th : i've been trying... (9 Replies)
Discussion started by: sasglm
9 Replies

7. Shell Programming and Scripting

String searching and output to a file in a formatted text

Hi, I'm very new to UNIX scripting and find quite difficult to understand simple UNIX syntax. Really appreciat if somebody could help me to give simple codes for my below problems:- 1) I need to search for a string "TTOH 8031950001" in a files which filename will be "*host*'. For example, the... (3 Replies)
Discussion started by: cuji
3 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

Perl: searching for a string in a file...

Hi All, I need to search for a string in a file that I've opened and base a decision on the result. The logic is this: "if the word 'Shared' appears on the first line then do this on the whole file else do this on the whole file " The code I currently have isn't working:... (4 Replies)
Discussion started by: pondlife
4 Replies

10. UNIX for Dummies Questions & Answers

Hi! Searching for a text string in UNIX

Hi! I'm new here and glad to meet everyone! I've been wrestling with a problem lately however! How do I recursively (recursive means to keep going through the subdirectories until no more are there) search a bunch of textfiles in a long directory structure for a specific string.. but only... (1 Reply)
Discussion started by: skwadim
1 Replies
Login or Register to Ask a Question