in reply to String substitution

If I understand correctly, you can probably achieve your desired output by removing the \b elements in your substitution. \b only matches at a word boundary, and it sounds like you don't want that.

Also, a couple notes on your code... $_ is aliased to each element of your array when using map or a for loop, so there's no need to do something like @lines = map { s/...//; $_ } @lines; ... You can just say s/...// for @lines;

If you're reading and writing to the same file, you can use the -i option on the command line. So you could probably shorten your entire code to this:

perl -pi.bak -e 's/apple/orange/gi' c:\data

This will overwrite your original file, substituting 'apple' with 'orange' wherever it is found, and will make a backup copy with a .bak extension in case that's not what you wanted.

-- Mike

--
just,my${.02}

Replies are listed 'Best First'.
Re^2: String substitution
by Anonymous Monk on Aug 09, 2006 at 19:59 UTC
    Is there a way to use: perl -pi.bak -e 's/apple/orange/gi' c:\data w/o the '-pi.bak' part; without making a backup of the file
      The p and the i are seperate options. You definitely want to keep the p, and you definitely want to keep the i ("inplace"), but if you don't specify an extention, the backup file isn't created. This is all documented in perlrun.
      perl -pi -e 's/apple/orange/gi'

      Caveat: i without an extention does not work in Windows.

Re^2: String substitution
by Anonymous Monk on Aug 11, 2006 at 17:23 UTC
    Is there a way to use: perl -pi.bak -e 's/apple/orange/gi' c:\data w/o the '-pi.bak' part