Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Need to search for a string in a file

by akrrs7 (Acolyte)
on Oct 20, 2011 at 10:17 UTC ( [id://932611]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I need to search for a certain string in a file. It has to be the first character in the line (if it is 2nd or later - don't get it). If the string is found, I have to get the value after the delimiter - (= equal). eg: Generated filename=a_b_c.xml Search for 'Generated filename' and return the value a_b_c.xml.Also G has to be the 1st character in the line. I have a basic code snippet:
my $logFile = "someFile.log"; my $stringToFind = "Generated filename"; open logFile, $logFile or die $!; my @list = grep /\b$stringToFind\b/, <logFile>; chomp @list; print "$_\n" foreach @list;
However, I am clueless how to ensure that G is the first character and to return the value after the equal sign. Thanks

Replies are listed 'Best First'.
Re: Need to search for a string in a file
by Utilitarian (Vicar) on Oct 20, 2011 at 10:43 UTC
    You needn't pull the whole file into memory just to search for one line.
    The caret character,(^), means start of the line in a regex. Read perlre, these meta-characters is the second subject covered.
    You can match save and exit the loop in one statement.
    sub get_file_from_log(){ my $log_name=shift; open(my $log_file, "<", $log_name); my $file_name; while (<logFile>){ last if ($file_name) = /^Generated filename\s*=\s*(.+)$/; } return $file_name; }

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      sub get_file_from_log(){ my $log_name=shift; open(my $log_file, "<", $log_name);

      Your prototype says to accept NO arguments but your next line says to accept ONE argument.    You should not use prototypes!

      You should always verify that open worked correctly before trying to use a possibly invalid filehandle.

      open(my $log_file, "<", $log_name); my $file_name; while (<logFile>){

      You are opening the filehandle $log_file but you are trying to read from the filehandle logFile?

      Hello Utilitarian...works like a charm. Thanks. I've read perlretut and perlre but still fumble. I will slowly get the hang as I keep writing code. Perl is not as intuitive to me as C++ and Java. Thanks a lot.....
        Perl is not as intuitive to me
        . You will find your way here. The terminology slightly is different, but there is always more than one way to do it in Perl, which provides extended expressibility.

      Will my @list = grep /re/, <FH> necessarily pull the entire file into memory? (I don't know and would like to know for sure!)

      It sure seems to!

      open my $fh, "<", "/dev/urandom" or die "$!"; my @a = grep /huh, whaddayamean/, <$fh>;

        Someone please correct me if I'm wrong, but I don't think your example will pull the entire file into memory. I think it will read lines from $fh one at a time, passing them to grep, which will return matching lines to @a.

        However, you're reading from /dev/urandom, which is an endless stream of random bytes, not a file which has an end. So eventually @a is going to get very large, yes. Also, depending on what your end-of-line delimiter is set to, <$fh> may return some very long lines for grep to deal with.

Re: Need to search for a string in a file
by moritz (Cardinal) on Oct 20, 2011 at 10:27 UTC
Re: Need to search for a string in a file
by Anonymous Monk on Oct 20, 2011 at 10:44 UTC
    perl -lne " print $1 if /^([^=]+)=/ " < infile  > outfile "

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-16 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found