Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

s/\r\n/\n/ passed as command line parameters not working

by ggrise (Initiate)
on Jan 17, 2013 at 17:49 UTC ( #1013841=perlquestion: print w/replies, xml ) Need Help??
ggrise has asked for the wisdom of the Perl Monks concerning the following question:

Hi wise perl monks, I have a question:
zentara in a prior post had supplied some code (below) which recursively replaced text in files. You called the program for example:
perl 'gaggle' 'geese' txt
and it works just fine replacing gaggle with geese in .txt files. However if I try to do a dos to unix file conversion
perl '\r\n' '\n' txt
it finds the windows "carriage return line feed" just fine but replaces that with the text \n instead of a "line feed".
if I replace the line:
$_ =~ s/$search/$replace/g;
$_ =~ s/\r\n/\n/g;
it does the dos to unix conversion ok
why ?
Thanks for your time.

#!/usr/bin/perl # by zentara # Recursively searches down thru directories replacing patterns in fil +es. # usage zsr 'search' 'replace' 'ext'(optional with no .) # use '' for null string in $replace # ex: zsr 'type1' 'series A' use warnings; use strict; use File::Find; my ($search,$replace,$ext) = @ARGV; if (defined $ext) {$ext = ".$ext"} else {$ext = '.*'}; die "Usage : zsr 'search' 'replace' 'extension' (extension optional)\n +" if ($search eq ""); find (\&wanted, "."); sub wanted { my $open = $_; my $tempfile = 0; if (!($open =~ /$ext$/i) or (-d||-B||-l)) {return} print $open,"\n"; my $mode = (stat $open)[2]; #print $mode,"\n"; #printf "Permissions are %04o\n", $mode & 07777; open (TEMP,">> $tempfile"); open (FH, "< $open") or die "Can't open $open: $!\n"; while (<FH>) { $_ =~ s/$search/$replace/g; print TEMP $_; } close FH; close TEMP; rename ($tempfile, $open) or die "Can't rename $open: $!\n"; chmod ($mode,$open) or die "Can't restore permissions to $open, possib +ly wrong owner: $!\n"; }

Replies are listed 'Best First'.
Re: s/\r\n/\n/ passed as command line parameters not working
by choroba (Bishop) on Jan 17, 2013 at 18:03 UTC
    The variable $replace contains two characters, \ and n. You can create a Perl expression from its value and evaluate it:
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks, that fixed the code. I appreciate your time and I learned something.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013841]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2018-05-26 15:53 GMT
Find Nodes?
    Voting Booth?