473,421 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Search Pattern And Arrays

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
3 1818
numberwhun
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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

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

Similar topics

4
by: Han | last post by:
Determining the pattern below has got my stumped. I have a page of HTML and need to find all occurrences of the following pattern: score=9999999999&amp; The number shown can be 5-10 characters...
8
by: Sharif T. Karim | last post by:
I am trying to do the following with my search script that looks for records in a mysql table. The following is an example of what I am trying to do. Text being searched: -- The brown fox...
5
by: Vamsee Krishna Gomatam | last post by:
Hello, I'm having some problems understanding Regexps in Python. I want to replace "<google>PHRASE</google>" with "<a href=http://www.google.com/search?q=PHRASE>PHRASE</a>" in a block of text....
6
by: cybit friendly | last post by:
Hi I'm stuck due to lack of knowledge. Problem: I'm scanning through Array1 to find the closest match to Array2 within Array1. At the moment I'm using a method that can best be described as...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
2
by: Alphonse Giambrone | last post by:
Is there a way to use multiple search patterns when calling Directory.GetFiles. For instance Directory.GetFiles("C:\MyFolder", "*.aspx") will return all files with the aspx extension. But what if...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
0
by: JosAH | last post by:
Greetings, last week we talked a bit about the Visitor design pattern. This week we'll talk a bit about additional functionality that is sometimes wanted, i.e. the functionality is optional....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.