On Fri, 13 Dec 2002, christopher j bottaro wrote: > hey, > i wanna make a perl script that will convert those stupid "\r\n" dos newlines > to good unix "\n" newlines...=) the problem isn't really a perl problem, but > a general programming problem that i've always kinda wondered about. > > so i guess what i'm gunna do is open the file, read in a line, search for > "\r\n", if its there, replace it with just "\n", then write the new (edited) > line to a new file. my problem is this...if the file is 10 megs, then not > only is the program gunna read a 10 meg file, but write one as well. is > there not a better way to do this? > > i can't really remove the "\r" in situ because as far as i understand, a file > is just an array of bytes and if i remove the "\r", i'd have to shift > everything else down one byte.
Perl has some switches that take care of the details of efficiently iterating over a file line by line for you, and even editing in place (well it isn't doen in place, but with a copy first). The -p switch says iterate over the entire file and print ecah lline after your script has run. -n does the same thing, but does not print the line. Add -i to either of these switches and the fiel is changed in place. The -e switch lets you write one line scripts. Converting \r\n to \n is simple with a regular expression s/\r\n/\n/g. Put it all together and you can do your crlf conversion trivially. Here I examine a file to see that it's contents indeed have dos line endings: [cgalpin@rabbit tmp]$ od -c dosfile.txt 0000000 l i n e 1 \r \n l i n e 2 \r \n 0000020 l i n e 3 \r \n \r \n 0000032 If dos2unix is not handy, perl to the rescue: [cgalpin@rabbit tmp]$ perl -pi -e 's/\r\n/\n/g' dosfile.txt Verify our work: [cgalpin@rabbit tmp]$ od -c dosfile.txt 0000000 l i n e 1 \n l i n e 2 \n l i 0000020 n e 3 \n \n 0000026 perl -pi.bak would have saved a backup as dosfile.txt.bak This one liner is the equivalent of creating a file with --- begin file contents -- #!/usr/bin/perl -pi s/\r\n/\n/g; --- end file contents -- If you want to keep it in your toolbox. hth charles -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]