in reply to Word Replacing

First of all, Perl is case sensitive, and it's not "PERL".

If you just want to replace a word, you don't even need a script:

perl -pi.orig -e 's/original/substitute/g' <filename>
Update: If you want to get your script working, you should iterate over all lines in the file, and not just work with the first line.

Each time you read something from <FILEHANDLE> in scalar context it reads one line.

Perhaps you should find a good book to learn Perl from.

Replies are listed 'Best First'.
Re^2: Word Replacing
by blazar (Canon) on May 31, 2007 at 13:40 UTC
    perl -pi.orig -e 's/original/substitute/g' <filename>

    (To the OP:) if you want that to apply to a large number of files, all in the same directory, just give them on the cmd line. That is, <filename> can really be <filenames> or a shell pattern that the shell will expand. If the files are too many for the shell to handle, then as I wrote in my other reply do so yourself with glob:

    perl -pi.orig -e 'BEGIN{@ARGV=map glob, @ARGV} s/original/substitute/g +' '*.foo' 'bar.*'

    Note that this is also necessary under Windows, where the shell does not do expansions at all. But in that case you have to use double quotes instead of single ones.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: Word Replacing
by xoddam (Novice) on May 31, 2007 at 13:29 UTC
    hmmm .. ok .. but if i want that it will search, check and replace all words in all files in a folder? look, i have for example about 1000 TXT files, and there is a word Bartholome and i need to replace it for Richard - In ALL files in this folder ... Yes i know .. I do perl mostly for fun, but i need it now .. :(
          look, i have for example about 1000 TXT files, and there is a word Bartholome and i need to replace it for Richard

      Similar to what was already pointed out to you:

      perl -spi -e 's/Bartholome/Richard/g' /path/to/folder/*


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      hmmm .. ok .. but if i want that it will search, check and replace all words in all files in a folder?

      Use glob or File::Find (if you want to do that recursively) or one of its friendlier relatives.