Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

Replies are listed 'Best First'.
Re: Grep n awk
by ww (Archbishop) 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 (Abbot) 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 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.
Re: grep(1) like functionality in Perl
by Anonymous Monk on Jan 03, 2013 at 16:13 UTC
    ## cat ; perl # 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 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 --

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011465]
Approved by ansh batra
[Lady_Aleena]: shmem, I'm not sure how.
[shmem]: well, deleting is done with 'rm' :-P
[shmem]: you can break up the chain MP3::Tag->new($_)- >interpolate("\%S ") and test whether MP3::Tag->new($_) is true
[Lady_Aleena]: shmem, no to deletion.
[shmem]: can't help you there since that song is not on my bench
[Lady_Aleena]: shmem, I need to look at MP3::Tag to see how to get the date from one song.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-04-23 22:14 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.