in reply to no reaction on string replace

Another possible approach is a Perl one-liner at the DOS command line:

perl -pi.bak -e "s/test/replace/g;" filename.txt

Try it with unimportant test files first. The replacements will occur in the existing file, and the old contents should be saved to a .bak file.

Replies are listed 'Best First'.
Re^2: no reaction on string replace
by semipro (Novice) on Sep 10, 2013 at 21:49 UTC
    Hi, thnx. Yeah thats working!!! cool
Re^2: no reaction on string replace
by semipro (Novice) on Sep 11, 2013 at 19:22 UTC
    Hi, ok I was bit early ecouraged. the line  perl -pi.bak -e "s/test/replace/g;" filename.txt works as DOS command. But when I try to squeez it in my skript he needs an operator before the file path. So
    #!/usr/bin/perl -w perl -pi.bak -e "s/test/replace/g;" C:/user/Desktop/filename.txt
    is not working. Can you help me out, again? Thank you!

      Yeah, on the DOS or UNIX command line, the "-pi.bak -e" options have the effect of considerably simplifying the syntax for modifying a text file.

      If you want to write a real script, it is slightly more complicated, those simplifications no longer apply.

      Within a Perl script, you would need to do something like this;

      #!/usr/bin/perl; use strict; use warnings; my $file_in = shift; open my $FH, "<", $file_in or die "cannot open $file_in $!"; while (<$FH>) { s/test/replace/g; print; }

      The above might still be considered simplistic, it is just giving an idea. Please don't hesitate to ask if you need further information.