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

Hi all, I trying to do a script that when every time I execute, it will open all .txt file one by one and scan if there the critical detect for error. Any idea with how can I open file and scan all the .txt and detect if inside .txt file name "critical" got error?

Replies are listed 'Best First'.
Re: how to open all file one by one
by graff (Chancellor) on Apr 12, 2013 at 02:52 UTC
    Are you sure you need perl for this? It sounds like all you need is the unix/linux/macosx "grep" utility (available for windows - just google "ms-windows grep")
    grep critical *.txt
    That will show you every line from every *.txt file that contains the word "critical". If all you want is the list of file names, use the "-l" option:
    grep -l critical *.txt
    If there's more you want to do, perl might be useful for that (or there might be another unix/linux/osx tool that does what you want...)
      :)ack is a perl replacement for grep
        it looked like a bit difference. I want to know how to open every file and scan inside the file word which if detect "critical :xxxxx" then will send out this file for the admin.
Re: how to open all file one by one
by hdb (Monsignor) on Apr 12, 2013 at 06:40 UTC
    use strict; use warnings; my @txtfiles = glob "*.txt"; my @criticalfiles; for my $file (@txtfiles) { my $fh; if( not open $fh, "<", $file ) { print "Cannot open file $file, skipping.\n"; next; } while( <$fh> ) { if( /critical/ ) { push @criticalfiles, $file; last; } } close $fh; } print "Critical files:\n"; print join( "\n", @criticalfiles ), "\n";
      Thanks, this script list out which txt file got critical word, but what I want is to list out the data inside the txt file. Can you advise me? :)
        Yes.

        I advise you to do some reading of documents (perlintro might be a good place to start); to study some of the relevant tutorials here; to use bigG or Super Search to find threads dealing with similar question; and most especially. to do some work on your own. Then ask again, if you get stuck with an honest effort.

        This is NOT a freee code-writing service. PerlMonks exists to help you learn.

        If you didn't program your executable by toggling in binary, it wasn't really programming!