CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
printing only the pattern that matches

 



keirnna
Novice

Jan 16, 2007, 8:30 PM

Post #1 of 15 (1758 views)
printing only the pattern that matches Can't Post

I did read this: http://perlguru.com/gforum.cgi?post=9166 in the FAQ section, but I am still lost.

I am trying to display only the line that ends with a . or , or ? that has the in the line that ended with .|,|?

So here is my pattern-matching operator:


Code
m/the/ .. /(\.|\?|\,)$/im


I am not sure if that is the correct pattern to look for, but if it is how do I extract just what matches that?

For example if I had

I wish it was the that dog barked,
but it wasn't.
It was a cat.

I need to only print "the dog barked,"


KevinR
Veteran


Jan 16, 2007, 10:33 PM

Post #2 of 15 (1755 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post

one possibility:


Code
my $string = qq~I wish it was the dog that barked, 
but it wasn't.
It was a cat.~;

print $1 while ($string =~ m/(the.*[?,.])$/img);

-------------------------------------------------


(This post was edited by KevinR on Jan 16, 2007, 10:34 PM)


keirnna
Novice

Jan 16, 2007, 11:14 PM

Post #3 of 15 (1747 views)
Re: [KevinR] printing only the pattern that matches [In reply to] Can't Post


Code
#!/usr/bin/perl -w 

use strict;
use warnings;

my $file = $ARGV[0];

open(INPUT, $file)
or die "Couldn't open $file for reading: $!\n";

while (<INPUT>) {
m/(it.*[?,.])$/img;
print $1;
}
close(INPUT);


That is what I have. What am I doing wrong?


keirnna
Novice

Jan 16, 2007, 11:48 PM

Post #4 of 15 (1746 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post

I also tried this with no luck:


Code
#!/usr/bin/perl -w 

use strict;
use warnings;

my $file = $ARGV[0];

open(INPUT, $file)
or die "Couldn't open $file for reading: $!\n";

print $1 while ($file =~ m/(it.*[?,.])$/img);

close(INPUT);



KevinR
Veteran


Jan 17, 2007, 10:31 AM

Post #5 of 15 (1742 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post


Code
my $file = $ARGV[0] || 'test.txt';   

open(INPUT, $file)
or die "Couldn't open $file for reading: $!\n";

while (<INPUT>) {
if (m/(the.*[?,.])$/i) {
print "$1\n";
}
}
close(INPUT);

-------------------------------------------------


KevinR
Veteran


Jan 17, 2007, 6:35 PM

Post #6 of 15 (1736 views)
Re: [KevinR] printing only the pattern that matches [In reply to] Can't Post

with this test file:


Code
I wish it was the dog that barked, 
but it wasn't.
It was a cat.



and this code:


Code
my $file = $ARGV[0];    

open(INPUT, $file)
or die "Couldn't open $file for reading: $!\n";

while (<INPUT>) {
if (m/(the.*[?,.])$/i) {
print "$1\n";
}
}
close(INPUT);


the output is:


Code
dog that barked,


are you trying to match across multiple lines?
-------------------------------------------------


keirnna
Novice

Jan 17, 2007, 8:20 PM

Post #7 of 15 (1734 views)
Re: [KevinR] printing only the pattern that matches [In reply to] Can't Post

No I want to match on a line by line basis. I also need to include the "the." In what is printed out.

Here is another example:


Code
my $file = $ARGV[0];    

open(INPUT, $file)
or die "Couldn't open $file for reading: $!\n";

while (<INPUT>) {
if (m/(it.*[?,.])$/i) {
print "$1\n";
}
}
close(INPUT);


Here is the file I want to scan:

Quote
Tim created the Module List in August 1994 and maintained it manually till
April 1996. By that time Andreas had implemented the Perl Authors Upload
Server (PAUSE) and it was happily feeding modules through to the CPAN archive sites.
Since PAUSE held a database of module information which could be maintained by module authors
it made sense for the module listing part of the Module List to be built
from that database. In April 1996 Andreas took over the automatic posting of
the Module List and I now maintain the other parts of the text. We plan to add
value to the automation over time.


I need this to output only: "it was happily feeding modules through to the CPAN archive sites."


stupeas
Novice

Jan 18, 2007, 12:17 AM

Post #8 of 15 (1731 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post

This thread is confusing. What is PAUSE? , Is it the string between the PAUSE's that you need?


keirnna
Novice

Jan 18, 2007, 12:23 AM

Post #9 of 15 (1728 views)
Re: [stupeas] printing only the pattern that matches [In reply to] Can't Post


In Reply To
This thread is confusing. What is PAUSE? , Is it the string between the PAUSE's that you need?


Pause has nothing to do with anything. It is in the file I am scanning.


KevinR
Veteran


Jan 18, 2007, 10:02 AM

Post #10 of 15 (1718 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post


In Reply To
No I want to match on a line by line basis. I also need to include the "the." In what is printed out.

Here is another example:


Code
my $file = $ARGV[0];    

open(INPUT, $file)
or die "Couldn't open $file for reading: $!\n";

while (<INPUT>) {
if (m/(it.*[?,.])$/i) {
print "$1\n";
}
}
close(INPUT);


Here is the file I want to scan:

Quote
Tim created the Module List in August 1994 and maintained it manually till
April 1996. By that time Andreas had implemented the Perl Authors Upload
Server (PAUSE) and it was happily feeding modules through to the CPAN archive sites.
Since PAUSE held a database of module information which could be maintained by module authors
it made sense for the module listing part of the Module List to be built
from that database. In April 1996 Andreas took over the automatic posting of
the Module List and I now maintain the other parts of the text. We plan to add
value to the automation over time.


I need this to output only: "it was happily feeding modules through to the CPAN archive sites."


works for me, using your file and the above code.
-------------------------------------------------


KevinR
Veteran


Jan 18, 2007, 11:45 AM

Post #11 of 15 (1715 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post

You should let it be known you have posted the same question on a different forum, otherwise people takng the time to answer could be wasting their time. I am speaking only for myself, but it's fairly common etiquette on forums to do so.

Thanks.
-------------------------------------------------


keirnna
Novice

Jan 18, 2007, 4:36 PM

Post #12 of 15 (1711 views)
Re: [KevinR] printing only the pattern that matches [In reply to] Can't Post

Yes I posted this question in TWO forums. Why? Because I need help, and no one in the first forum was giving me any help. Is that a problem? Am I wasting your time? I am just asking for some help.


KevinR
Veteran


Jan 18, 2007, 4:45 PM

Post #13 of 15 (1710 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post

No, you're not wasting my ime, I'm glad to help you with your question. Have you got it working yet? It works for me.
-------------------------------------------------


keirnna
Novice

Jan 18, 2007, 4:50 PM

Post #14 of 15 (1709 views)
Re: [KevinR] printing only the pattern that matches [In reply to] Can't Post

No it still isn't working for me. When I execute it I just get a new command line waiting for input.


KevinR
Veteran


Jan 19, 2007, 12:36 AM

Post #15 of 15 (1705 views)
Re: [keirnna] printing only the pattern that matches [In reply to] Can't Post


In Reply To
No it still isn't working for me. When I execute it I just get a new command line waiting for input.



hmmm.... what are you typing at the command line to get the program started?
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives