Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to ignore lines that match certain string after it is matched for the first time within a same file

by Ben328 (Novice)
on Sep 19, 2012 at 23:04 UTC ( #994559=perlquestion: print w/ replies, xml ) Need Help??
Ben328 has asked for the wisdom of the Perl Monks concerning the following question:

I have lines as follows in a file

Today is amazing.nice day Today is amazing.nice but still I haven't done anything Yesterday was just a normal day Tomorrow probably will be amazing.new day

What i want to accomplish : I want to be able to go through lines and match "(amazing.*)". If (amazing.*) is unique then put it in an array and also print that line if not ignore it. Basically, this is the result I am looking for.

Today is amazing.nice day Tomorrow probably will be amazing.new day
and so forth....

So far I have only written this much code. I just don't know where to go from here. Any direction on how to approach this would be very much appreciated.

#!/usr/bin/perl -w use strict; while ( my $line = <> ) { if ( $line =~ m/(\(amazing.+?\))/) { my $string="$1"; @allstrings = ($string); } }

Comment on How to ignore lines that match certain string after it is matched for the first time within a same file
Select or Download Code
Re: How to ignore lines that match certain string after it is matched for the first time within a same file
by ikegami (Pope) on Sep 19, 2012 at 23:09 UTC
    my %seen; s/(amazing\S+)/ $seen{$1}++ ? ...new... : $1 /eg;
Re: How to ignore lines that match certain string after it is matched for the first time within a same file
by Kenosis (Priest) on Sep 19, 2012 at 23:47 UTC

    Perhaps the following will work for you:

    use strict; use warnings; my ( @array, %seen ); while ( my $line = <DATA> ) { if ( $line =~ /(amazing\.\S+)/ and not $seen{ lc $1 }++ ) { push @array, $line; #print $line; } } print for @array; __DATA__ Today is amazing.nice day Today is amazing.nice but still I haven't done anything Yesterday was just a normal day Tomorrow probably will be amazing.new day

    Output:

    Today is amazing.nice day Tomorrow probably will be amazing.new day

    $1 captures amazing\.\S+ and using lc on the hash key helps insure case is not significant in uniqueness.

    Hope this helps!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994559]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-12-29 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls