Perl - Grep function regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Grep function regular expression
# 1  
Old 08-18-2010
Perl - Grep function regular expression

For some reason,
@logs is a list of log files
@filter is a list of expressions to grep out
Code:
foreach (@logs){
open READ, "<$_" or die $!;
@temp=<READ>;
close READ;

    foreach (@filter){
        print grep /$_/,@temp ;    
        
}
}

returns a regex error in one of the files it's parsing.

while
Code:
print grep $_,@temp ;

returns the entire file

while
Code:
print grep /string/,@temp ;

works fine.


I can not seem to wrap my head around it today. Any clues?
# 2  
Old 08-19-2010
what u wanna get actualy?
you just opening every file and reading it then trying to grep some filter-shit from it's content..
is it what u want?
i'm not sure if grep can work for the whole array..
why not to do it when u actualy opened the file and while($line=<FILE>){grep it} ?
anyway try to use $somevar, not $_
tip78
# 3  
Old 08-19-2010
Try to use the list context , ex (not tested):

Code:
foreach (@logs){
open READ, "<$_" or die $!;
@temp=<READ>;
close READ;

    foreach $var (@filter){
          @u=(@u,grep /$var/,@temp );    
        
}
}

foreach $u (@u){ print "u:".$u."\n"; }

# 4  
Old 08-19-2010
Everything you have checked is right.. Understand as mentioned below.

Quote:
Originally Posted by adelsin
For some reason,
@logs is a list of log files
@filter is a list of expressions to grep out
Code:
foreach (@logs){
open READ, "<$_" or die $!;
@temp=<READ>;
close READ;

    foreach (@filter){
        print grep /$_/,@temp ;    
        
}
}

returns a regex error in one of the files it's parsing.
When the file has a line with just single open paranthesis, then you will get regex error ?! What is the big deal in this ?

If you want to avoid then escape it using either quotemeta function, or use \Q. Using some input directly as regex is not good, as you have this risk.
Quote:
Originally Posted by adelsin
while
Code:
print grep $_,@temp ;

returns the entire file
What does grep will do ? If for current iteration the return value is true then the current $_ will be pushed in to the result array, so you get the entire array, as each time you have some value at $_ and that returns true.

Quote:
Originally Posted by adelsin
while
Code:
print grep /string/,@temp ;

works fine.






I can not seem to wrap my head around it today. Any clues?

If you dont understand the basics, you will end up in confusions.
# 5  
Old 08-19-2010
Quote:
When the file has a line with just single open paranthesis, then you will get regex error ?! What is the big deal in this ?
It would exit the program at this point.

I ended up having to go with passing $_ to a scalar variable and then placing the scalar variable into an array.

Code:
foreach (@logs){
open READ, "<$_" or die $!;
@temp=<READ>;
close READ;
    foreach (@filter){
        my $alt=$_;
        print grep {/\Q$alt\E/i} @temp ;    
        }
}

Thank you all for your help.


Quote:
If you dont understand the basics, you will end up in confusions.
This is why I'm here. I was able to figure out the problem after I thought about what was happening for awhile and researched how the grep() function operated. And remembered about Quotemetas.


Just out of curiosity, when I do the following:
Code:
foreach (@logs){
open READ, "<$_" or die $!;
#@temp=<READ>;
        foreach (@filter){
        my $alt=$_;
        print grep {/\Q$alt\E/i} <READ>;    
        }
close READ; 
}

It seems not to go through the @filter array, but rather only the first value of it. Why would this be when trying to read directly from the file?
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 regular expression

I want to track only below: I am using below, but it doesn't work: (6 Replies)
Discussion started by: proactiveaditya
6 Replies

2. Shell Programming and Scripting

Grep + Regular expression or

Hi , I have few lines like A20120101.ANU.ZIP A20120401.ABC.ZIP A20120105.KJK.ZIP A20120809.JUG.ZIP A20120101.MAT.ZIP B20120301.ANU.XIP I want to filter by 1. Files starting with A and Ending With Z ( ^A.*.ZIP$) 2. And either ANU, or KJK or MAT in the file name. Hope my... (6 Replies)
Discussion started by: Anupam_Halder
6 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

5. UNIX for Advanced & Expert Users

regular expression for split function in perl

Hi, Below is an example of a record I have, which I wish to split using the perl's split function and load it into an array. I am having tough time figuring out the exact reg-ex to perform the split. Given record: "a","xyz",0,2,48,"abcd","lmno,pqrR, stv",300,"abc",20, The delimiter to... (4 Replies)
Discussion started by: jghoshal
4 Replies

6. Shell Programming and Scripting

Help with grep / regular expression

Hi, Input file: -13- -1er- -1xyz1- -1xz12- -2ab1- -2ab2-- -143- Code: grep '^*\-' input.txt Wrong output: -13- -1xyz1- -2ab1- -2ab2-- (4 Replies)
Discussion started by: dragon.1431
4 Replies

7. Shell Programming and Scripting

grep regular expression

please can someone tell me what the following regrex means grep "^aa*$" <file> I thought this would match any word beginning with aa and ending with $, but it doesnt. Thanks in advance Calypso (7 Replies)
Discussion started by: Calypso
7 Replies

8. Shell Programming and Scripting

grep with regular expression

Hi, guys. I have one question, hope somebody can give me a hand I have a file called passwd, the contents of it arebelow: *********************** ... goldsimj:x:5008:200: goldsij2:x:5009:200: whitej:x:5010:201: brownj:x:5011:202: goldsij3:x:5012:204: greyp:x:5013:203: ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

9. UNIX for Advanced & Expert Users

regarding grep regular expression

When i do ls -ld RT_BP* i am getting the following list. drwxrwx--- 2 user group 256 Oct 17 10:09 RT_BP809 drwxrwx--- 2user group 256 Oct 17 10:09 RT_BP809.O drwxrwx--- 2 user group 256 Oct 17 10:09 RT_BP810 drwxrwx--- 2user group 256 Oct... (2 Replies)
Discussion started by: ukatru
2 Replies

10. Shell Programming and Scripting

grep : regular expression

guys, my requirment goes like this: I have a file, and wish to filter out records where 1. The first letter is o or O and 2. The next 4 following letter should not be ther I do not wish to use pipe and wish to do it in one shot. The best expression I came up with is: grep ^*... (10 Replies)
Discussion started by: RishiPahuja
10 Replies
Login or Register to Ask a Question