From: Chris L. on
Dear All,
I am trying to match a specific pattern using grep.

my @results=grep(/$cominput/,@common);
print "@results";

Naturally,Grep is only printing the lines that variable '$cominput' is
found on. My dilemma is I want to print a pattern. Specifically, where
variable '$cominput' is found all the way until the character '<' is
found. Example,

<$cominput 7894596 isnlpop 78304 plmeitn 4457624 knmljebn
93643456346500 lfnsndh 235 hfgnsdljjtngf 7753 fhs 8694039486 jfsndfkgs
jsdhfnskdk 753299658 hgnsnd 2573275 nvndjgfi 847523
<
I would like to grep from "$cominput" to the character "<".

I have looked into the map function and several pattern matching
examples from google searches, ie:
while (<>) {
if (/BEGIN PATTERN/ .. /END PATTERN/) {
}
}

But nothing seems to be working. is there a better way to accomplish
this??
Thank you very much for your time.
Chris

From: usenet on
Chris L. wrote:
> I am trying to match a specific pattern using grep.
>
> I would like to grep from "$cominput" to the character "<".
>
> I have looked into the map function

You need to instead look at how Regular Expressions work (perldoc
perlre), such as (untested):

grep /$cominput.*</ @whatever;

--
http://DavidFilmer.com

From: usenet on
usenet(a)DavidFilmer.com wrote:
> grep /$cominput.*</ @whatever;

Waitaminute, that didn't answer the question - sorry. Too late I
noticed the OP specified multiple lines.

I don't believe you can grep over multiple lines, but I'm not certian
of that...

--
http://DavidFilmer.com

From: Jim Gibson on
In article <1139589597.697227.10460(a)g47g2000cwa.googlegroups.com>,
Chris L. <chrisliapis(a)gmail.com> wrote:

> Dear All,
> I am trying to match a specific pattern using grep.

You do not use grep to match a specific pattern, you use the match
operator m//. You use grep to apply a test to the elements of an array
and return only those elements for which the test is true.

If you want to match a multi-line piece of text against a pattern, then
you can put the multi-line text into a scalar variable. Then you can
match that text against a pattern. Use the 's' modifier for the match
operator so that the '.' pattern metacharacter will match the newlines
in your multi-line text.


#!/usr/local/bin/perl
use strict;
use warnings;

local $/;
my $data = <DATA>;
if( $data =~ m/<\$cominput(.*)</s ) {
print "matched: |$1|\n";
}
__DATA__
<$cominput 7894596 isnlpop 78304 plmeitn 4457624 knmljebn
93643456346500 lfnsndh 235 hfgnsdljjtngf 7753 fhs 8694039486 jfsndfkgs
jsdhfnskdk 753299658 hgnsnd 2573275 nvndjgfi 847523
<

.... which produces:

matched: | 7894596 isnlpop 78304 plmeitn 4457624 knmljebn
93643456346500 lfnsndh 235 hfgnsdljjtngf 7753 fhs 8694039486 jfsndfkgs
jsdhfnskdk 753299658 hgnsnd 2573275 nvndjgfi 847523
|

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
From: Eric Schwartz on
Just a minor tweak to your regex:

Jim Gibson <jgibson(a)mail.arc.nasa.gov> writes:
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> local $/;
> my $data = <DATA>;
> if( $data =~ m/<\$cominput(.*)</s ) {

You probably want:

if( $data =~ m/<\$cominput([^<]*)</s ) {

there instead. Try running both versions against a __DATA__ section
that looks like:

<$cominput 7894596 isnlpop 78304 plmeitn 4457624 knmljebn
93643456346500 lfnsndh 235 hfgnsdljjtngf 7753 fhs 8694039486 jfsndfkgs
jsdhfnskdk 753299658 hgnsnd 2573275 nvndjgfi 847523
<
<$cominput 7894596 isnlpop 78304 plmeitn 4457624 knmljebn
93643456346500 lfnsndh 235 hfgnsdljjtngf 7753 fhs 8694039486 jfsndfkgs
jsdhfnskdk 753299658 hgnsnd 2573275 nvndjgfi 847523
<

and see why. :)

-=Eric
 |  Next  |  Last
Pages: 1 2 3 4
Prev: use library
Next: random fortune cookie