script help request
Joe Smith
jes at martnet.com
Sat Oct 7 01:56:16 UTC 2006
Paul Lemmons wrote:
> Try this. I am sure it could be done better but I think it will get the
> job done ...
Not "better" just different:
# Read pairs like "81230 jsmith" from stdin
%id = map {split} <>;
foreach (<*_d_[0-9]*>) {
# substitute the id#: $_ is orig filename, $n is new
($n = $_) =~ s/_d_([0-9]+)_/_d_$id{$1}_/;
# rename the file only if the id# is in our list
if ($id{$1}) {
rename($_,$n) || warn "can't rename $_: $!\n";
} else {
warn "unknown id '$1' in file '$_'\n";
}
}
Or just for fun, as a one-liner:
%id=map{split}<>;($n=$_)=~s/_d_([0-9]+)_/_d_$id{$1}_/ && \
$id{$1} && print("$_>$n\n") || warn "$_:$!\n" foreach(<*_d_[0-9]*>);
More information about the users
mailing list