Search Pattern And Arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search Pattern And Arrays
# 1  
Old 01-23-2008
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 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.
# 2  
Old 01-29-2008
If it's just a matter of determining the number of arguements passed.
Try using 'shift' and iterate through them.
Otherwise, 'foreach $var, @ARGV' should work too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

7. Shell Programming and Scripting

1. search 2nd pattern after a pattern and summarize stats

I have two questions. I am sure one of the Guru will be able to help either one or both. 1. Find 2nd occurance of pattern= "Bind variable after pattern="ABN USER Admin" ...... ABN USER Admin <--- I know this string ..... Bind variable ... .. Bind variable <-- Want to print this... (4 Replies)
Discussion started by: ran123
4 Replies

8. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question