I develop on both Windows and Unix laptops for deployment on Unix servers, and invariably forget to save text files from Windows in Unix-format (in Windows, lines end with both the line feed and carriage return ASCII characters, but Unix uses only a line feed). It doesn’t cause any problems, but it’s sure ugly to look at on the server. Googling around, I found the following solution using the
> mv file file.tmp > tr -d '\r' < file.tmp > file > rm file.tmp
After putting up with what was really more of an ugly hack for about a year, I decided it was time to write a quick and dirty shell script to hide the mess – but before reinventing the wheel thought I’d try a quick ‘net search. After all, I can’t be the first one to do this, could I?
Asking Google again for how to convert text from DOS to Unix, but this time including “shell script”, I mostly got the same links to tr, sed, and perl tutorials that I got the first time. There were also a couple links to shell scripts, but almost hidden in the hits was a reference to “flip”, a ~250 line C program that has apparently been the de facto standard command since it was released into the wild in 1998.
flip does a lot more than I was going to implement in my quick and dirty shell script. It detects binary files and leaves them alone unless intentionally overridden. It doesn’t modify files that are already in the specified format, and it preserves file timestamps. It handles user interrupts gracefully and doesn’t leave behind garbage or corrupted files.
Compiling and installing flip from the FreeBSD ports collection took all of 20 seconds. I don’t know why I didn’t find flip a year ago, but think I’ll dig out my venerable Unix in a Nutshell and see what other utilities are out there I could use, but don’t know about.