By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Search Pattern And Arrays

P: 8
Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3.  
  4. #use strict;
  5. use warnings;
  6.  
  7.   sub search_pattern
  8.       {
  9.  
  10.           my $file_name = $_[0];
  11.  
  12.           my $search = $_[1];
  13.  
  14.           open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
  15.  
  16.           while (<LOGFILE>)
  17.                 {
  18.  
  19.  
  20. if ( $_ =~ /$search/ ) {
  21.  
  22. my $val = $`;  #Matches Everything after pattern
  23.  
  24. $val =~ s/^\s+//; #remove leading spaces
  25. $val =~ s/\s+$//; #remove trailing spaces
  26. $val =~ s/\D//g;  #Just has the digits. All other charcters are filtered.
  27.  
  28. #print "$val\n";
  29. print "\nFirst Occurence:$val \n";
  30.  
  31. my $line = $.;
  32. print "Line number:$line\n";
  33.  
  34. #$temp = $line;
  35. #print "$temp";
  36. #print "$.";
  37.  
  38. last;
  39. }
  40. }
  41. }
  42.  
  43. my $file_n ="test.txt";
  44.  
  45. my $search_p = "This is phrase 2";
  46. &search_pattern($file_n, $search_p);
  47.  
  48.  
OUTPUT
------

First Occurence:90
Line number:3

Hi guys,

My code above searches for the first occurence of a phrase and returns the line number...

What if i wanna search for multiple search phrases in a single file and return the line numbers.

For example:
test.txt

1.This is phrase 1
2.This is phrase 3
3.This is phrase 2
4.This is phrase 3

any text in between

5.This is phrase 5

any text in between

6.This is phrase 4
7.This is phrase 1
8.This is phrase 2

9.This is phrase 3
10.This is phrase 6
11.This is phrase 7
12.This is phrase 1
13.This is phrase 2
14.This is phrase 3
15.This is phrase 4
16.This is phrase 8
17.This is phrase 1

.................
.................

i have given the line numbers for my reference in reality, it is not present...

The arguments passed are file_name and Search phrases (for eg: this is phrase1, this is phrase 2, This Phrase 3, phrase 5...)

The number of arguments passed may vary... But the first argument is always the file_name.

first it will check for phrase 1 and then from that line number phrase 2 (even though in our case Phrase 3 come in between phrase 2 ie., line 2 we need phrase 2 first and then phrase 3) and then phrase 3 and return phrase 3.

lets say we have 4 arguments including file name...

if (phrase 1 exists) ---------- if (phrase 1 doesnt exists)

from that line number ----------- search for phrase 2

search for phrase 2 ---------- if (phrase 2 also doesnt exists)

from that line number ---------- search for phrase 2 and return phrase 3

---------if (phrase 3 also doesnt exists)

----------- return phrase 2

----------- else phrase 1

search for phrase 3 ----------- if none of them exists display a message

return the phrase 3


the reason iam keeping track of line number is so that the file is not parsed from line 1.

if i know the number of arguments passed then its fine...

But the thing is what if i dont know the number of arguments presents.

can anybody suggest how do i proceed for this problem.
Jan 23 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
I can help you with the number of arguments. And for the answer to that, you can look here.

Regards,

Jeff
Jan 23 '08 #2

P: 8
I can help you with the number of arguments. And for the answer to that, you can look here.

Regards,

Jeff
thanks Jeff...
can u give me some links or resources for learning arrays in depth for Perl?

regards,
Mercury.
Jan 23 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
thanks Jeff...
can u give me some links or resources for learning arrays in depth for Perl?

regards,
Mercury.
Check your PM's (Private Messages).

Regards,

Jeff
Jan 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.