Perl one liners...

Here are ten one liners in perl, illustrating the kinds of task one would use a script for under linux. All the scripts take some kind of argument after the final ' quote that signals the end of the program.

1. perl -p -i -e 's/this/that/g' filename

Search and replace the string 'this' with the string 'that' in the file filename. You can also say * or *.html or any valid wildcard expression instead of filename. The s/// command uses regular expressions. If you want to alter the 'this' and 'that', it's best to avoid .*?[ ]{}$^ and other odd characters which act as metacharacters in regular expressions. Or better still look at the perlre documentation page. You can do this by saying perldoc perlre. Perl has extensive online documentation, try perldoc perltoc for a list.

2. perl -e 'for (@ARGV) { rename $_, lc($_) unless -e lc($_); }' *

Rename all the files in the current directory to lower case. The $_ is a sort of 'default' variable which is widely used in Perl.

3. perl -e 'for (@ARGV) { rename $_,$_.'l' unless -e lc($_); }' *

Add an 'l' on the end of every filename in the current directory. That is: .htm => .html. The 'unless -e' statement means 'unless the filename exists'.

4. perl -MLWP::Simple -e 'mirror("http://www.perl.com/" , "perl.html")'

Copy a file across the Web if it is newer than the local copy. You will need to install the "libnet" and "libwww" bundles to make this work. The LWP package comes with a great documentation page'lwpcook' which has lots of examples of other ways to transfer data across the WWW with Perl.

5. perl -p -i -e 's/'

Convert Unix files to DOS files. Unix files have a different end of line character sequence from DOS.

6. perl -e 'if (!fork) {sleep $ARGV[0]*60; print "aaa" ; exit;} exit;' 1

Wait for the number of minutes specified and then beep. Note the use of the fork() to run in the background. Use the linux commnd 'ps' if you want to watch the command running.

7.perl -e 'use Socket; $name =gethostbyaddr(inet_aton($ARGV[0] ),AF_INET); print $name;' 207.153.253.38

Convert the given domain name or dotted IP number to a hostname. It will convert whatever you throw at it to a sensible and consistent hostname.

8. perl -MTime::Local -e '$y2k=timelocal(0,0,0,1,0,2000); $until=$y2k-time; print "seconds $until to y2k ";'

This finds the number of seconds until the year 2000 is upon us.

9. perl -e '$n=utime ((stat($ARGV[0]))[8,9], @ARGV) ;print $n' aaa t*

Make all files beginning with the letter t have the same time stamp as the file 'aaa'

10. perl -l -e 'open(F,"/usr/dict/english"); $w=join("",sort split(//,$ARGV[0])); print grep {chop;join("",sort split(//,$_)) eq $w} ;' life

Find all anagrams of the word 'life'.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Replace spaces in file names with underscores

Here's a one liner with bash to convert file names with spaces and replace it with underscores instead.

$ for filename in ./*; do mv "$filename" $(echo -n $filename | tr " " "_") ; done
Comment