nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Search Pattern And Arrays

This is a discussion on Search Pattern And Arrays within the Coding in General forums, part of the Development/Scripting category; Code: #!/usr/bin/perl #use strict; use warnings; sub search_pattern { my $file_name = $_[0]; my $search = $_[1]; open(LOGFILE, $_[0]) or ...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-23-2008, 10:47 PM
Junior Member
User
 
Join Date: Jan 2008
My distro: Debian
Posts: 2
Rep Power: 0
mercuryshipz is on a distinguished road
Default Search Pattern And Arrays

Code:
#!/usr/bin/perl

#use strict;
use warnings;

  sub search_pattern
      {

          my $file_name = $_[0];

          my $search = $_[1];

          open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");

          while (<LOGFILE>)
                {


if ( $_ =~ /$search/ ) {

my $val = $`;  #Matches Everything after pattern

$val =~ s/^\s+//; #remove leading spaces
$val =~ s/\s+$//; #remove trailing spaces
$val =~ s/\D//g;  #Just has the digits. All other charcters are filtered.

#print "$val\n";
print "\nFirst Occurence:$val \n";

my $line = $.;
print "Line number:$line\n";

#$temp = $line;
#print "$temp";
#print "$.";

last;
}
}
}

my $file_n ="test.txt";

my $search_p = "This is phrase 2";
&search_pattern($file_n, $search_p);
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 exist ---------- if (phrase 1 doesnt exist

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

search for phrase 2 ---------- if (phrase 2 also doesnt exist

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

---------if (phrase 3 also doesnt exist

----------- 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.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-11-2008, 02:26 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

Code:
#!/usr/bin/perl

#use strict;
use warnings;

sub search_pattern
{
    my ($file_name, @search) = @_;
    open(LOGFILE, $_[0]) or die("Error: cannot open file '$_[0]'\n");
    while (<LOGFILE>) {
        foreach $pattern (@search) {
        if ( $_ =~ /$search/ ) {
Is this what you want ?

Last edited by agn; 02-12-2008 at 11:13 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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

Similar Threads

Thread Thread Starter Forum Replies Last Post
word search in file chandanperl Coding in General 2 03-11-2008 10:16 AM
pattern search with numbers mercuryshipz Coding in General 0 02-14-2008 11:16 PM
How to use arrays and its values present in one script? Nishanthhampali Shell scripting 0 01-31-2008 02:04 PM
diff command to exclude files that match pattern piggy Getting started tutorials 1 08-13-2007 10:52 PM
How to find different type of words (pattern matching) in vi sweta Linux software 2 06-07-2005 10:22 PM


All times are GMT +5.5. The time now is 12:34 AM.


Powered by vBulletin® Version 3.7.2 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36