sdslrn123 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Exit a program if there are any matching lines?

Replies are listed 'Best First'.
Re: Exit a program if there are any matching lines?
by Corion (Patriarch) on Jun 27, 2006 at 09:01 UTC

    You mean, a while loop, going through files, and then calling exit if a line matches ?

    Please, tell us what you've done so far and show some effort and code. The following snippet will do what you say you want, but it won't do what you want I guess.

    perl -ne "exit if /matching/"

    You might want to read How (Not) To Ask A Question to help us improve the quality of our responses.

Re: Exit a program if there are any matching lines?
by marto (Cardinal) on Jun 27, 2006 at 09:04 UTC
Re: Exit a program if there are any matching lines?
by reasonablekeith (Deacon) on Jun 27, 2006 at 09:04 UTC
    The monks here generally don't like to spoon feed, you've got to show you've made an effort first.

    What exactly are you having trouble with? Opening the file? Reading each line? Matching against the line? From your previous posts, you know how to do atleast some of this. So have a stab at it, and post your results back here.

    Cheers, Rob

    ---
    my name's not Keith, and I'm not reasonable.
Re: Exit a program if there are any matching lines?
by strat (Canon) on Jun 27, 2006 at 09:03 UTC

    This could look about like the following piece of code:

    #! /usr/bin/perl use warnings; use strict; my $file = shift(@ARGV); unless( defined $file or -f $file ) { die "Usage: $0 filename\n"; } # unless open( my $FH, "<", $file ) or die "Error: couldn't open file '$file': $!\n"; while( defined my $line = <$FH> ) { chomp $line; if( $line =~ /something/ ) { print "Line found in $.: $line\n"; last; } # if } # while close( $FH );

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Exit a program if there are any matching lines?
by wfsp (Abbot) on Jun 27, 2006 at 09:14 UTC
Re: Exit a program if there are any matching lines?
by ambrus (Abbot) on Jun 27, 2006 at 10:19 UTC

    Run cgrep, my grep clone, with the -q or -l or -L option, any of which makes the program stop reading when a matching line is found. Then, look at the source if that's what you want.