From: Don Armstrong Date: Wed, 3 Jan 2007 00:20:55 +0000 (+0000) Subject: add sgf_rename X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=aea2662b394ab870fec3e92bc647d3d68273770a;p=bin.git add sgf_rename --- diff --git a/sgf_rename b/sgf_rename new file mode 100755 index 0000000..193a434 --- /dev/null +++ b/sgf_rename @@ -0,0 +1,40 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use IO::File; + +for my $sgf (@ARGV) { + my %sgf; + my $fh = new IO::File $sgf, 'r' + or warn "Unable to open $sgf for reading: $!" + and next; + while (<$fh>) { + chomp; + my ($field,$value) = $_ =~ /^([A-Z]{2})\[([^\]]+)\]/; + next unless defined $field; + if ($field =~ /^[WB]R$/) { + $value =~ s/\s+dan/d/; + $value =~ s/\s+pro/p/; + $value =~ s/\s+kyu/k/; + } + $sgf{$field} = $value; + } + undef $fh; + my $new_file = join('_', + map {s/\s+/_/g; + s/,//g; + s/[_-]+/_/g; + s/^_//; + s/_$//; + lc($_); + } + map{defined $_?($_):()} + (@sgf{qw(EV RO PB BR)},'v', + @sgf{qw(PW WR DT RE)} + ) + ).'.sgf'; + next if $sgf eq $new_file; + warn "$new_file already exists" and next if -e $new_file; + rename($sgf,$new_file); +}