http://qs1969.pair.com?node_id=1222734

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

Hi,

I'm trying to replace strings in a text file but cannot get it to work using the following code:

system("sed -i -e 's/The action failed./failed_build/g' $lookuptxtfile +");

Any help modifying this code or using a better way to accomplish this would be greatly appreciated.

Replies are listed 'Best First'.
Re: Replace strings in text file
by choroba (Cardinal) on Sep 20, 2018 at 16:48 UTC
    open my $lookup, '<', $lookuptxtfile or die $!; open my $new_lookup, '>', "$lookuptxtfile.new" or die $!; while (<$lookup>) { s/The action failed./failed_build/g; print {$new_lookup} $_; } close $new_lookup; rename "$lookuptxtfile.new", $lookuptxtfile or die $!;

    Creating a new file and renaming it is what sed does behind the scenes, anyway.

    Are you sure you don't want to backslash the dot?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Replace strings in text file
by tybalt89 (Monsignor) on Sep 20, 2018 at 16:53 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1222734 use strict; use warnings; use Path::Tiny; my $lookuptxtfile = 'd.lookuptxtfile'; path($lookuptxtfile)->spew(<<END); one two The action failed. three The action failed. twice The action failed. four END print path($lookuptxtfile)->slurp; print "\n"; # end setup for testing do # actual code { local ($^I, @ARGV) = ('', $lookuptxtfile); print s/The action failed./failed_build/gr while <>; }; # end actual code :) print path($lookuptxtfile)->slurp; # debug print
Re: Replace strings in text file
by tybalt89 (Monsignor) on Sep 21, 2018 at 09:26 UTC

    And here's the Path::Tiny version...

    #!/usr/bin/perl # https://perlmonks.org/?node_id=1222734 use strict; use warnings; use Path::Tiny; my $lookuptxtfile = 'd.lookuptxtfile'; path($lookuptxtfile)->spew(<<END); one two The action failed. three The action failed. twice The action failed. four END print path($lookuptxtfile)->slurp; print "\n"; # end setup for testing # start actual code path($lookuptxtfile)->edit( sub { s/The action failed./failed_build/g +} ); # end actual code print path($lookuptxtfile)->slurp; # debug print
Re: Replace strings in text file
by haukex (Archbishop) on Sep 23, 2018 at 08:03 UTC

    TIMTOWTDI, if you don't mind me plugging one of my own modules, File::Replace:

    use warnings; use strict; use File::Replace 'replace'; my $lookuptxtfile = "lookup.txt"; my $fh = replace($lookuptxtfile); while (<$fh>) { s/The action failed./failed_build/g; print $fh $_; } close $fh;

    Or, if a single filehandle is too much magic for your taste:

    use warnings; use strict; use File::Replace 'replace3'; my $lookuptxtfile = "lookup.txt"; my ($infh,$outfh,$repl) = replace3($lookuptxtfile); while (<$infh>) { s/The action failed./failed_build/g; print $outfh $_; } $repl->finish;

    Note: This is a re-post of a node that was lost.

Re: Replace strings in text file
by thanos1983 (Parson) on Sep 21, 2018 at 08:11 UTC

    Hello TonyNY,

    Fellow Monks have provided you with a solution to your problem. Just for fun you can use also the module File::Slurp.

    Sample code:

    #!/usr/bin/perl use strict; use warnings; use File::Slurp qw(read_file write_file); my $filename = 'in.txt'; my $data = read_file $filename, {binmode => ':utf8'}; $data =~ s/The action failed\./failed_build/g; write_file $filename, {binmode => ':utf8'}, $data; __END__ $ cat in.txt one two The action failed. three The action failed. twice The action failed. four $ cat in.txt one two failed_build three failed_build twice failed_build four

    As fellow Monk choroba said File::Slurp is broken and wrong, instead you can use the File::Slurper.

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; use File::Slurper qw(read_text write_text); my $filename = 'in.txt'; my $data = read_text($filename); $data =~ s/The action failed\./failed_build/g; write_text($filename, $data); __END__ $ cat in.txt one two The action failed. three The action failed. twice The action failed. four $ cat in.txt one two failed_build three failed_build twice failed_build four

    Or mu personal favorite IO::All, sample of code:

    #!/usr/bin/perl use strict; use warnings; use IO::All -utf8; # Turn on utf8 for all io my $contents = io->file('in.txt')->slurp; $contents =~ s/The action failed\./failed_build/g; $contents > io('in.txt'); __END__ $ cat in.txt one two The action failed. three The action failed. twice The action failed. four $ cat in.txt one two failed_build three failed_build twice failed_build four

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Replace strings in text file
by pwagyi (Monk) on Sep 24, 2018 at 03:22 UTC

    What are you trying to accomplish here? Have you read man page of sed ?

    The sed utility is a stream editor that reads one or more text files, makes editing changes according to a script of editing commands, and writes the results to standard output.

    Since you are already using perl, why not just use perl to perform substitution (a lot of monks had already given out code) instead of *nix sed, then your code will be portable (across other OSes, like windows).