<?xml version="1.0" encoding="windows-1252"?>
<node id="1013841" title="s/\r\n/\n/ passed as command line parameters not working" created="2013-01-17 12:49:40" updated="2013-01-17 12:49:40">
<type id="115">
perlquestion</type>
<author id="1013462">
ggrise</author>
<data>
<field name="doctext">
&lt;p&gt; 
Hi wise perl monks, I have a question:
&lt;br&gt;
zentara in a prior post had supplied some code (below) which recursively replaced text in files. You called the program for example:
&lt;br&gt; 
perl zsr.pl 'gaggle' 'geese' txt
&lt;br&gt;
and it works just fine replacing gaggle with geese in .txt files. However if I try to do a dos to unix file conversion
&lt;br&gt;
perl zsr.pl '\r\n' '\n' txt 
&lt;br&gt;
it finds the windows "carriage return line feed"  just fine but replaces that with the text \n instead of a "line feed".
&lt;br&gt;
if I replace the line:
&lt;br&gt; 
$_ =~ s/$search/$replace/g;
&lt;br&gt;
with
&lt;br&gt;
$_ =~ s/\r\n/\n/g; 
&lt;br&gt;
it does the dos to unix conversion ok 
&lt;br&gt;
why ?
&lt;br&gt;
Thanks for your time.
&lt;/p&gt;
&lt;code&gt;
#!/usr/bin/perl
# by zentara
# Recursively searches down thru directories replacing patterns in files.
# 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 (\&amp;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 &amp; 07777;
open (TEMP,"&gt;&gt; $tempfile");
open (FH, "&lt; $open") or die "Can't open $open: $!\n";
while (&lt;FH&gt;) {
$_ =~ 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, possibly wrong owner: $!\n";
}
&lt;/code&gt;</field>
</data>
</node>
