Tech and travel

No dos2unix, no perl

2007-05-24

I was stuck on a machine with a lot of scripts that had the dreaded ^M at the end of each line. These were Windows files in a huge zip file, and it was a pain to use the FTP to move that zip file over again. Dos2unix wasn’t installed, nor was perl. One way out of this is by creating the following script, called transform.sh :

tr -d \\r <$1 >$1.fixed
mv $1.fixed $1

This run tr on the file, stripping out newline (\r), and saves this in a new file with the postfix .fixed. So install.sh becomes install.sh.fixed. The second line overwrites the original file with this new version.

Then it’s just a matter of calling this for all the files :

find . -type f -name "*.sh" -exec /path/to/transform.sh {} \;

This finds all the files of type f, which means a normal file. The file also has to have the extension .sh. Then our script transform.sh is called on that file, passing in the filename, specified by {}, as a parameter.

BTW : the way to do this in Perl is :

perl –pi –e 's/^M//' *

The ^M is entered by pressing Ctrl-V followed by Ctrl-M .

Copyright (c) 2024 Michel Hollands