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

I am having trouble getting my script to search for at certain line in a file, and the replacing the following line. The reason I have to do it this way, is that the line I need to replace (LocalCompilerPresent=1) is listed severeal times, and it is only 1 of them I need to replace.

is it possible to search for the line before (LocalCompilerPresent=1), because that line is unique and the replacing the line after it?

It's only the 1 that has to be changed, but I can just replace LocalCompilerPresent=1 with LocalCompilerPresent=0..

Can anyone help me?

Replies are listed 'Best First'.
Re: Search and replace next line
by borisz (Canon) on Jan 05, 2005 at 09:53 UTC
    #!/usr/bin/perl while ( defined ( $_ = <> ) ) { print; if ( /^line before your replacement/ ) { <>; print "LocalCompilerPresent=0\n"; } }
    perl rep.pl <infile >outfile
    Boris
Re: Search and replace next line
by Thilosophy (Curate) on Jan 05, 2005 at 09:52 UTC
    while (<>) { print; if (m/TheLineBefore/){ # read the line to be replaced my $line = <>; # do something with it $line =~ s/1/0/; # print the new version print $line; } } __DATA__ test data more TheLineBefore LocalCompilerPresent=1 more stuff
    This is a frequent question, see also the search results for next line.

      Won't solve the problem.

      • the OP asked to replace the instance(s) of LocalCompilerPresent=1 after the first one.
        Your script will only work if the wanted line is in the line immediately following the searched one. If there is a blank line in between, it will fail.
      • Your script will print both the original and replaced lines.
        I replace the line following the searched line. blank or not. Thats what I understand from the question.
        And the script does NOT print both lines. Read the source again.

        UPDATE: For some reason I answered to the wrong Node! Please Ignore. Sorry to the anon monk.
        Boris
        Your script will only work if the wanted line is in the line immediately following the searched one.

        True. But I understood him as wanting to replace the immediately following line.

        Your script will print both the original and replaced lines.

        Really? What does it output for you? For me, it says

        test data more TheLineBefore LocalCompilerPresent=0 more stuff
        which I take to be correct. No?
Re: Search and replace next line
by perlnewbie2000 (Initiate) on Jan 05, 2005 at 10:48 UTC
    I think I forgot to tell you that it has to replace the line (LocalCompilerPresent=0) in th .ini file also.

    Sorry that I didn't mention that.

    So it has to search for a line, replace 0 in the following line with a 1 and save that in the file.

    Do I make sense whatsoever? :)
      So it has to search for a line, replace 0 in the following line with a 1 and save that in the file.

      You replace one line in a file by rewriting the whole file( at least that is the easiest way). So you can use one of the two snippets above (I believe both work, but there seems to be dissent, so check first) to read your .ini file and write the results to a new file. Check if it is good and then copy it over the original .ini file.

      If you feel confident in the script you can also automatically have Perl overwrite the input file with the -i switch.

      perl -i rep.pl config.ini
        I'm having some trouble getting it to work.
        Will you look at my code and see what could be wrong?

        $cs="d:\\Perl\\Compiler_Setup.ini"; open cs or die "Cannot open $cs for read :$!"; while (<cs>) { if (m/"Gimpel PC-Lint for H8]"/){ # read the line to be replaced my $line = <cs>; # do something with it $line =~ s/1/0/; # print the new version print $line; } }

        Right now it just doesn't do anything.
        in a command prompt I write
        perl "replace test.pl"
        and then it just goes back to d:\perl> as if it ran the program, and then just goes back to where I was before. It says:

        D:\Perl>perl "replace test.pl"

        D:\Perl>
Re: Search and replace next line
by perlnewbie2000 (Initiate) on Jan 05, 2005 at 13:24 UTC
    I have found out of it..
    Thanks for your help. It's very appreciated..
    and thanks for the very fast replies.