You mean something like this?

while(<INFILE>){
  push @matches,$_ =~ /(silly)/;
}

foreach(@matches){
        print $_."\n";
}

-----Original Message-----
From: Harry Putnam [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 06, 2002 4:18 PM
To: [EMAIL PROTECTED]
Subject: print only what a regex actually hits


Hope this isn't too often repeated question with glaringly obvious
answer:
How to print only what a regex hits from a file, not the whole line.

IMPORTANT: I don't want techniques involving call back (remembered)
operators and parens, I know how to piece those together for simple
things like the file below.

Is there not a straight forward way to make perl print only what a
regex sees?  I'm thinking of someting along the line of gnu awk
(gawks) ability to do it, as in defining RS (record separator) to a regex
then printing its content which appears in the builtin RT.
(example of awk below this example file)
 
Example file:
cat file

a line of print
a line of text
a line of blab
a silly line of print
a line of text
a silly line of blatterscat
a line of nonsense
a line of pure intellect

What perl code will go thru this file and print only what is hit by
regex /silly/?


Using the awk I mentioned and barring odd blanks one gets from
redefining the RS:
 awk 'BEGIN{RS="silly"}{print RT}' file
 silly
 silly
 <blank line> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to