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

dear perlmonks, I have no idea why this is not working, I don't know if it has to do with the file read (<) operator or what. I want to substitute a pattern using the s/// command. I know it is finding the pattern but will not substitute. Keep in my mind that this script opens up another file and looks for this string and then substitutes it with "". Basically I believe that this is the way to delete words with a text file. But either way I can't seem to get this code to work.
$file = '\\\daaecrmpd\d$\dba\freespacemonitor\directory.txt'; open(FILE, "<$file") or die "can't open $file: $!"; while (<FILE>){ s/total_bytes/""/ig; } close FILE;

Replies are listed 'Best First'.
Re: Substitution in a file
by swiftone (Curate) on Jan 11, 2001 at 21:44 UTC
Re: Substitution in a file
by fundflow (Chaplain) on Jan 11, 2001 at 22:04 UTC
    You can do the following:
    perl -i.orig -pe 's/total_bytes/""/ig' myfile.txt
    To change the file and back up the old one with .orig extension.

    Cheers.

Re: Substitution in a file
by OeufMayo (Curate) on Jan 11, 2001 at 22:09 UTC

    If your script only does a susbstitution, you can also use a one-liner:

    perl -pi.bak -e 's/total_bytes/""/ig' directory.txt #*nix perl -pi.bak -e "s/total_bytes/\"\"/ig" directory.txt #Win32
    <kbd>--
    PerlMonger::Paris(http => 'paris.pm.org');</kbd>
Re: Substitution in a file
by davorg (Chancellor) on Jan 11, 2001 at 22:15 UTC

    One other thing I've just noticed. Do you really want to replace the string 'total_bytes' with two double quotes or do you want to replace it with an empty string? If it's the latter, then you should use:

    s/total_bytes//ig;
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Substitution in a file
by davorg (Chancellor) on Jan 11, 2001 at 21:45 UTC

    You're reading in the data, processing it, but then just throwing it away. You probably want to write out the data to another file.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me