Tag Archives: Perl

Solving problems in seconds

Sometimes a given solution to a problem lies much closer at hand than you expect. In my work I usually do the same task repeatedly with between 6 and 50 files. Even though Unix is very efficient in many ways, this still takes time to do by hand. I have thought of various ways around that problem, including using wildcards (*), but never got fully satisfied. But this week, I finally came up with the simplest solution this far. And it took about a minute or two to implement. I don’t know why I didn’t think about this a year ago. Maybe I thought that I would only do these repetitive tasks a couple of times. I was wrong, but thanks to Perl I can now be much more efficient (and write this instead of typing Unix commands…) The good thing about my implementation (in my opinion) is that it’s so flexible. Here’s my code, please comment if you feel that there is more efficient ways. “{}” is replaced by a number for each file name:


#!/usr/bin/perl

## LOOP COMMAND
$versionID = "Version 1.0";
print "LoopCommand\n";
print "Version $versionID\n";
print "Written by Johan Bengtsson, October 2009\n";
print "-----------------------------------------\n";

## GET USER INPUT
print 'Execute command: ';
chomp($command = <STDIN>);
print 'From number: ';
chomp($start = <STDIN>);
print 'To number: ';
chomp($end = <STDIN>);

## EXECUTE
for ($i = $start ; $i <= $end ; $i++) {
$exec = $command;
$exec =~ s/\{\}/$i/g;
$result = `$exec`;
print $result;
}