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

'XYZ' appears in hundreds of locations in a file. I want to replace this string where ever it appears in this file, with say 'ABC'. Can anyone help me with this? I had like a 2 line perl script that someone had given me, which worked great! But I can't remember it anymore. I appreciate your help and this site! - yh.

Replies are listed 'Best First'.
Re: Search & Replace
by Zaxo (Archbishop) on Sep 16, 2002 at 06:25 UTC
    perl -pi -e's/XYZ/ABC/g' thefile.name anotherfile.name

    After Compline,
    Zaxo

      And for the paranoid and the careful: perl -pi.bak -e's/XYZ/ABC/g' thefile.name anotherfile.name which will leave behind copies of the original files with .bak appended to their names.

      Makeshifts last the longest.

        for the paranoid and the careful:

        Good advice, but don't make the same mistake I made once and run that twice to fix a typo or buglet.

        In that case, of course, the "wrong" versions generated by the first run will overwrite the .bak files.
        --
        Mike

Re: Search & Replace
by fuzzyping (Chaplain) on Sep 16, 2002 at 06:25 UTC
    perl -pe 's/XYZ/ABC/' file1.pl > file2.pl
    -fp