rasputin.dnsalias.com

Perl Tricks

Posted on November 8, 2008 10:26 AM

I use Perl a lot when I am programming something. I have a bad habit of figuring out how to do something and then forgetting how I did it later, which sends me to Google trying to figure out the "trick" again.

When I think of it, I'll put my Perl tricks here, and hopefully I won't have to spend so much time trying to figure it out the next time!

Remove Windows Returns From Text Files
Windows uses two characters for the end of line in text files, but Unix only uses one of those characters. When you open a Windows saved file in Unix or some editors even on Windows you may see a funny character at the end of each line that looks like ^M You can use perl to remove these extra characters using a couple command switches for the perl interpreter:

Code:

perl -p -i -e 's/\r//g' filename.txt


-e tells perl that the entire program will be on the command line ('s/\r//g' in this case, a regular expression that replaces all '\r' with '')

-i tells perl to edit the files in place, this means that the command will CHANGE the file (use with caution!) If you do not include this one, perl will send the output to STDOUT so you can send it to a new file. i.e. >newfile.txt

-p tells perl that it should run the program for each filename argument

So to get rid of the ^M characters in all .asp files in a directory you would run:

Code:

perl -p -i -e 's/\r//g' *.asp


More info about Perl and command switches can be found here:
http://ftp.sunsite.ualberta.ca/Documentation/Misc/perl-5.6.0/pod/perlrun.html

(Updated 11/17/2008)
Here's another one that I like to use:

If I have database Data Definition Language (DDL) that creates tables and I want to grab the table names that are created, I use this one:

Code:

perl -n -e 'print "$1\n" if (m/create table\s+(\w+\.\w+)/i);' tables.ddl


I am sure that the code can be cleaned up quite a bit, but I am too lazy to tackle combining the multiple search and replaces to make one regular expression. I will leave that as an exercise for the reader. :)
Add/View Comments

Tags: