Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

2009-04-16

Perl one liner for dos2unix

So we have all come across the situation: you copy a bunch of files from DOS/Windows to Unix and the newline characters are all messed up (in other words, you see a "^M" at the end of each line). So the scripts you wrote lovingly, are all messed up now.

There are two choices:

Use dos2unix - this will change the newline characters one file at a time. Trouble is, that dos2unix writes to stdout, so you'd have to pipe the output to a temporary file and move it back to your script...

Use the following perl one-liner:

perl -pi -e 's/\r\n/\n/g' *

You guessed it - Windows/DOS uses "\r\n" for newline, while Unix uses only "\n"

2005-04-12

Perl - delete empty directories

The Program

use File::Find;
finddepth(sub{rmdir},'.')

But wait, there's more!

To save the time of copying that to a file before running it, use Perl's lazy feature whereby one can run a whole program from a string. Simply, on the command prompt, type

perl -MFile::Find -e"finddepth(sub{rmdir},'.')"



Cool, huh?!