in reply to Open a file, search for a string and fill a string in another file

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

Replies are listed 'Best First'.
Re^2: Open a file, search for a string and fill a string in another file
by ramki067 (Acolyte) on Jan 23, 2014 at 09:11 UTC
    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? } } } }