Wednesday, April 9, 2008

Perl on the fly

To replace string in a file on the fly using perl simply enter in the command line the following:

% perl -p -i -e 's/one/two/g' file

The script simply replace all entries of "one" with "two".

find command

Friday, April 4, 2008

renaming files using perl

This is an example of how to rename a set of files that match a particular pattern.

use File::Find;
use File::Path;

die "usage:$0 (DIR)\n" unless @ARGV;
$dir = $ARGV[0];

finddepth(\&edits, $dir);

sub edits {
$fname = $_;
if (-f) {
if ($fname =~/[^\s].\.(cnf|dnf|eps|etat|planches)$/) {
print "Scanning $fname\n";
#$newname = $fname;

my @values = split(/\./, $fname);
#foreach my $val (@values) {
#print "$val\n";
my $pref=@values[0];
my $suff=@values[1];
$newname="test_" . $pref . "." .$suff;
print "$newname\n";
rename ($fname, $newname)


File line ending from DOS to UNIX or MAC

Alt+x set-buffer-file-coding-system

Short cut : CTRL X ret F