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

I have 2 text files. I"m writing a perl script wherein i need to find "Unable to parse" string in a text file and then extract the whole line having this string. Extract part of this string after "/" and store the string in a variable. Then i need to open the other text file, find the stored string in this text file and replace the string.
my $ldir = "/Android"; $RESULTS_FILE = $ldir.'/'.'results.html'; open OUT, ">>", $RESULTS_FILE; open(IN,"<logcat.txt"); while(<IN>) { chomp; if( $_ =~ m/Unable to parse/ ) { my @string = split('/',$_); print @string; my $stream_name = $string[4]; while $srch(<OUT>) { chomp; if( $srch =~ m/$stream_name/ ) { // How to replace the line here? } } } }
Please help. Regards, Ramki
  • Comment on Open a file, search for a string and fill a string in another file
  • Download Code

Replies are listed 'Best First'.
Re: Open a file, search for a string and fill a string in another file
by davido (Cardinal) on Jan 23, 2014 at 08:20 UTC
Re: Open a file, search for a string and fill a string in another file
by McA (Priest) on Jan 23, 2014 at 08:10 UTC

    Hi,

    you described the flow of your program pretty well, so you have to open the file for reading (perldoc -f open) and one for writing the output to, then you read the file line by line (perldoc -f readline) in a loop (perldoc perlsyn). With every line you serach for the pattern via regex (perldoc perlre). If the pattern matches you do another pattern match or split which depends what is easier to extract the text (perldoc -f split). When you have the extracted portion you print the text using the output file handle to the output file (perldoc -f print). When you have read the entire file (end of loop) then you close both filehandles (perldoc -f close).So, you see, pretty straight forward.

    When you encounter concrete problems on the way, come back and ask, because almost anybody here will help.

    UPDATE: Your initial question has been changed. Please mark updates as updates otherwise all answers given by the monks may sound totally stupid as they don't fit the request seen by the others. So, the solution which comes to my mind in your case is using system and calling there sed or the perl equivalent to substitute the found line in one rush.

    Best regards
    McA

      I"ve written the code for the above, but i'm stuck at a point to replace the line the result file with the line i want. Please help. Below is my code:
      my $ldir = "/Android"; $RESULTS_FILE = $ldir.'/'.'results.html'; open OUT, ">>", $RESULTS_FILE; open(IN,"<logcat.txt"); while(<IN>) { chomp; if( $_ =~ m/Unable to parse/ ) { my @string = split('/',$_); print @string; my $stream_name = $string[4]; while $srch(<OUT>) { chomp; if( $srch =~ m/$stream_name/ ) { // How to replace the line here? } } } }
Re: Open a file, search for a string and fill a string in another file
by kcott (Archbishop) on Jan 23, 2014 at 16:06 UTC

    G'day ramki067,

    Given your $RESULTS_FILE is a web page, and therefore not of inordinate length, Tie::File might be a good option here.

    In place of "open OUT, ...", use

    tie my @results, 'Tie::File', $RESULTS_FILE;

    and, in place of "while $srch(<OUT>) {...}", use

    for (@results) { last if s/search/replace/; }

    Additionally, there's a few issues with your code:

    • You're opening a file in append mode but you only read it in the code shown. See open for details.
    • You're not checking whether your I/O is successful. open also has information on this; alternatively, you may find it easier to just use the autodie pragma.
    • You'd also do well to check the syntax of while.

    -- Ken