% perl -p -i -e 's/one/two/g' file
Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts
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:
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.
#!/usr/bin/perl
use File::Find;
use File::Path;
die "usage:$0 (DIR)\n" unless @ARGV;
$dir = $ARGV[0];
finddepth(\&edits, $dir);
exit(0);
sub edits {
$fname = $_;
if (-f) {
if ($fname =~/[^\s].\.(cnf|dnf|eps|etat|planches)$/) {
print "Scanning $fname\n";
#$newname = $fname;
#$newname=~s/(.\./test_$1/;
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)
}
}
}
#!/usr/bin/perl
use File::Find;
use File::Path;
die "usage:$0 (DIR)\n" unless @ARGV;
$dir = $ARGV[0];
finddepth(\&edits, $dir);
exit(0);
sub edits {
$fname = $_;
if (-f) {
if ($fname =~/[^\s].\.(cnf|dnf|eps|etat|planches)$/) {
print "Scanning $fname\n";
#$newname = $fname;
#$newname=~s/(.\./test_$1/;
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)
}
}
}
Wednesday, March 26, 2008
Remove leading and trailing whitespace
To remove leading and trailing whitespace(or any trailing char):
- Regular Expression :
- s/^\s+//;
- s/\s+$//;
- Perl :
- Chomp();
- Python:
- strip();
Subscribe to:
Posts (Atom)