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

Hi

I use the following code to open a text file and update any strings such as "apple" to orange".

open (IN,"c:\\data"); @lines = <IN>; close IN; @lines = map { s/\b$old_hostname\b/$new_hostname/sgi; $_ } @lines; open (OUT,">c:\\data") || "can't open data :$! "; print OUT @lines;
But now I must change any words that that have "apple" as a substring to any words that have "orange" as a substring. For example "data/applered" or "new_apple_dark" becomes " data/orangered" "new_orange_dark" and so on .

How can I alter my substitution to make it work.

--thanks kirk

Replies are listed 'Best First'.
Re: String substitution
by Paladin (Vicar) on Apr 17, 2003 at 19:16 UTC
    If you don't want to match against word boundries \b, then remove those constraints from the regex. ie.
    s/$old_hostname/$new_hostname/sgi

    Note: You may also want to add \Q and \E around your $old_hostname if you don't want any special regex characters in there to affect the match.

    s/\Q$old_hostname\E/$new_hostname/sgi
Re: String substitution
by thelenm (Vicar) on Apr 17, 2003 at 19:24 UTC
    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}

      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.

      Is there a way to use: perl -pi.bak -e 's/apple/orange/gi' c:\data w/o the '-pi.bak' part
Re: String substitution
by dpuu (Chaplain) on Apr 17, 2003 at 19:18 UTC
    You need to remove the \bs. (as an aside: you can also replace map with foreach):
    foreach (@lines) { s/apple/orange/g }
    --Dave