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

The output of the following command is:

perl -ne "print if /\\bnapre/i || /\\bjupiter/i;" GPPRegistry.temp.reg "DBLogin"="napre" "DSN"="jupiter" "DBLogin"="napre" "Login"="napre" "Password"="napre" "DBLogin"="napre"

I want in the same command to replace "napre" with "TEST1" and present it (Not change the file yet). after this command the fields include "napre" are despaired and the replacement of "napre" weren't be shown

perl -ne 's/napre/Text1/gi; print if /\bnapre/i || /\bjupiter/i;' GPPR +egistry.temp.reg "DSN"="jupiter" "DSN"="jupiter"

20071115 Janitored by Corion: Removed color, font tags, added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: perl in one line: Retrive & Replace
by ikegami (Patriarch) on Nov 13, 2007 at 21:21 UTC
    napre has been changed to Text1, so searching for napre is useless.
    perl -ne 's/napre/Text1/gi; print if /\bText1/i || /\bjupiter/i;' GPPR +egistry.temp.reg

    If you want to check if the original line contains napre, check before doing the replacement.

    perl -ne 'next if !(/\bnapre/i || /\bjupiter/i); s/napre/Text1/gi; pri +nt' GPPRegistry.temp.reg

    By the way, putting your code in <c>...</c> tags will format it properly, handle escaping for you and handle wrapping if necessary.

Re: perl in one line: Retrive & Replace
by igelkott (Priest) on Nov 13, 2007 at 21:36 UTC
    You didn't find "napre" because you replaced it with "TEST1" before the search. OK, you probably noticed that but it's sometimes easy to overlook the obvious. The above doesn't change the file (as the "-i" option would do) but it does change the line you're reading. Simply search for test1 (as well as jupiter).