Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can I Grep multiple lines??

Reply
Thread Tools

Can I Grep multiple lines??

 
 
Chris L.
Guest
Posts: n/a
 
      02-10-2006
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

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      02-10-2006
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

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      02-10-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      02-10-2006
Just a minor tweak to your regex:

Jim Gibson <(E-Mail Removed)> 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
 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      02-10-2006

Chris L. wrote:
> 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??


if i understand your problem correctly, this may work:
while (<>) {
/($cominput[^<]*?)</ && print $1, "\n";
}

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-10-2006
"Chris L." <(E-Mail Removed)> wrote:
> 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.


It is returning lines that match the *contents* of variable $cominput when
such contents are interpreted as a regex.


> My dilemma is I want to print a pattern. Specifically, where
> variable '$cominput' is found all the way until the character '<' is
> found. Example,


Then change the definition of a line such that they are terminated by "<"
rather than by "\n".

$/='<';

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Xicheng
Guest
Posts: n/a
 
      02-10-2006
Chris L. wrote:
> 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/) {
> }
> }

If you are working on arrays instead of files, you may try map instead
of grep, coz the later does nothing on your array elements..
===========
use strict; use warnings;
use Data:umper;
my @array = (
'sfdsgfdhgdh<$cominput 7894596 isnlpop 78304 plmeitn 4457624
knmljebn
93643456346500 lfnsndh 235 hfgnsdljjtngf 7753 fhs 8694039486 jfsndfkgs
jsdhfnskdk 753299658 hgnsnd 2573275 nvndjgfi 847523 <dsfgdsg ',
'asdf<< afsef
asefsdfg <$cominput ',
'sfdsgfdhgdh<$cominput dfdsg
111
222 < < ',
'
jsdhfnskdk 753299658 hgnsnd 2573275 nvndjgfi 847523 <dsfgdsg ',
);
print Dumper \@array;
my @tmp = map {/<\$cominput([^<]*)</} @array;
print Dumper \@tmp;
==============
Best,
Xicheng

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


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      02-10-2006
Eric Schwartz schreef:
> Jim Gibson:


>> m/<\$cominput(.*)</s

>
> m/<\$cominput([^<]*)</s


m/<\$cominput(.*?)</s

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-11-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> I don't believe you can grep over multiple lines,



Of course you can.

grep() filters a list.

If the elements of that list contain multiple lines, then grep()
will select on multiple lines.


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

my @list = ("line\n1\n", "line\n2\n");
foreach my $selected ( grep /2/, @list ) {
print ">>>$selected<<<";
}
-------------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-11-2006
Chris L. <(E-Mail Removed)> wrote:

> I am trying to match a specific pattern using grep.
>
> my @results=grep(/$cominput/,@common);



What is the value in $cominput?

We must know what the pattern is if we are to help.

What are the values in @common?

We must know what strings the pattern match will be applied
to if we are to help.


> print "@results";
>
> Naturally,Grep is only printing the lines that variable '$cominput' is
> found on.



From the data that you show below '$cominput' is a _string_
not a variable.


> My dilemma is I want to print a pattern.



I doubt that you want to print a pattern.

I expect that yo want to print the part of a string that
matches a pattern.


> Specifically, where
> variable '$cominput'



You mean the _string_ '$cominput' (I think).


> is found all the way until the character '<' is
> found. Example,



Then write a pattern that matches all the way until the
character '<' is found.


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



Is that really what your data looks like?

I thought you said that grep() was matching something, but grep()
won't match anything with what I assume your pattern is.

I must assume what your pattern is because you have not told
us what your pattern is.


> I would like to grep from "$cominput" to the character "<".



You don't need grep() for that.

You need a pattern match for that:

print "$1\n" if $string =~ /(\$cominput[^<]*)/;


Note that the dollar sign is escaped, because I think you
are talking about a literal dollar sign, rather than one that
functions as a sigil on a Perl scalar variable.


> But nothing seems to be working.



If you show us what you tried (in a form that allow _us_ to try
it too) then we could help you fix it.

But you didn't, so we can't.


> is there a better way to accomplish
> this??



A "better way" implies that you have discovered at least one way.

I get the impression that you do not have _any_ ways of accomplishing "this".


> Thank you very much for your time.



Your post is so chock full of inconsistencies that loose (incorrect)
terminology that I cannot divine what you want, nor what you have
tried.

I cannot answer your incomprehensible question.

Have you seen the Posting Guidelines that are posted here frequently?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you get the 1st occurrence of a value using grep? Mmcolli00 Mom Ruby 3 05-14-2009 09:30 PM
Can I beat perl at grep-like processing speed? js Python 4 01-03-2007 03:30 PM
Re: Can I beat perl at grep-like processing speed? Tim Smith Python 1 12-29-2006 04:15 PM
Multiple File Grep Blake Perl Misc 3 07-27-2004 04:35 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM



Advertisments