Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Grep n awk

by Anonymous Monk
on Jan 03, 2013 at 15:04 UTC ( #1011465=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

abcd dg 17786 sam mixed -- abcd -- 17889 ram -- -- abcd dg 17786 dam mixed --

How can I grep "mixed" from these ommiting the lines that having pattern like "-- --" ,i.e, two --.

I have to do it in a perl script . Please Help

Comment on Grep n awk
Re: Grep n awk
by Anonymous Monk on Jan 03, 2013 at 15:06 UTC
    Actaully lines are like

    abcd dg 17786 sam mixed --

    abcd -- 17889 ram -- --

    dg 17786 dam mixed --

Re: Grep n awk
by ww (Bishop) on Jan 03, 2013 at 15:16 UTC
    • perldoc -f grep
    • perldoc perlretut

    What have you tried? How did it fail? What messages (errors and/or warnings) did your code produce?

    Oh, you do know, don't you, that this isn't a free coding service? We try to help those who help themselves. See also On asking for help (and not just "by the way," Markup in the Monastery or, at least, the directions surrounding the text-input box where you created your node).

Re: Grep n awk
by kennethk (Monsignor) on Jan 03, 2013 at 15:46 UTC

    In addition to what ww asked, why do you "have to do it in a [P]erl script"? If I were to pick a program to accomplish your task, I'd use grep.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: grep(1) like functionality in Perl
by Anonymous Monk on Jan 03, 2013 at 16:13 UTC
    ## cat p.pl ; perl p.pl # This is "grep -f" like. use v5.10.0; use strict; my @in = ( "polka\n" , "avoid redding dot me\n" , "dot me\n" , "yellow\n" ); my $find = 'me'; my $avoid = 'avoid'; for my $ignore ( $avoid , undef ) { process_input( $find , $ignore , @in ); print "- - - -\n"; } exit; sub process_input { my ( $grep , $skip , @in ) = @_; my $show; while ( my $line = shift @in ) { $show = find( $grep , $line ) && ( defined $skip ? ! find ( $skip , $line ) : 1 ) ; } continue { $show and print $line; } return; } sub find { my ( $find , $line ) = @_; defined $find && defined $line or return; my @break = split '' , $find; my $size = scalar @break; my $start = 0; my ( @seen ); for my $c ( @break ) { my $i = index $line , $c , $start; $i == -1 || scalar @seen == $size and last; push @seen , $i; if ( scalar @seen > 1 ) { $seen[-1] - $seen[-2] == 1 or last; } $start = $i + 1; } return scalar @seen == $size } __END__ dot me - - - - avoid redding dot me dot me - - - -

      I had made a grave error when I declared ...

      ... # This is "grep -f" like. ...

      ... for there was nothing of that sort. I should have just written fgrep instead of futzing with option name to indicate grep to run in fixed string search mode. To repent, comment should have been ...

      ... # This partially tested program has limited # fgrep-like behaviour. ...
Re: Grep n awk
by space_monk (Chaplain) on Jan 03, 2013 at 17:23 UTC

    This answer is a bit rushed as I'm in a hurry to go home for a beer, but you should get the idea.

    # slurp the file.... my @lines = <$fh>; # filter the lines.... my @filter1 = grep( /mixed/, @lines); my @filter2 = grep( !/-- --/,@filter1);

    A Monk aims to give answers to those who have none, and to learn from those who know more.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-10-24 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls