CoCalc Public Fileswww / sga / sga / scripts / sga.plOpen in with one click!
Author: William A. Stein
1
# sga.pl -- perl script, William Stein ([email protected])
2
# run by typing the following at a command prompt:
3
# perl sga.pl
4
#
5
# Example:
6
# [[email protected] file_names]$ perl sga.pl
7
# SGA filename tool, version 0.1
8
# by William Stein ([email protected])
9
# First input file: 2a09.tif
10
# String B: 2_
11
# First (3-digit) number XYZ: 009
12
# cp 2a09.tif 2_009.tif
13
# cp 2a10.tif 2_010.tif
14
#
15
##############################################################################
16
# I would like you to write (whenever it's convenient) a tool that will give #
17
# the files we have sensible names. Recall that our files have names (for, #
18
# say, SGA 1) of the form 1AXY.tif, where A is a lower-case letter and X and #
19
# Y are two digits. I'd like to be able to convert these to the form #
20
# BXYZ.tif, where B is some fixed string (e.g. "1_" or "4.5_") and X,Y, and #
21
# Z are digits. So, for instance, the files of SGA 1 could be changed from #
22
# 1a01.tif ... 1i45.tif to 1_001.tif ... 1_375.tif. #
23
# #
24
# More precisely, I'd like it to do this: #
25
# #
26
# Inputs: first input file, string B, first (3-digit) number XYZ #
27
# Output: starting with the first input file and proceeding in #
28
# alphabetical order, rename the files to BXYZ.tif, increasing the #
29
# number XYZ once each time #
30
##############################################################################
31
32
33
print "SGA filename tool, version 0.1\n";
34
print "by William Stein (was\@mail.math.berkeley.edu)\n";
35
36
# will need the "chr NUMBER" function
37
38
sub increment {
39
my $n = length($_[0]);
40
my $prefix=substr($_[0],0,n-7);
41
my $letter=substr($_[0],n-7,n-6);
42
my $num=substr($_[0],n-6,n-4);
43
$num++;
44
my $new = $prefix.$letter.$num.".tif";
45
if ($num > "99") {
46
$num = "01";
47
$letter = chr(ord($letter)+1);
48
if (ord($letter) == ord("z")+1) {
49
exit;
50
}
51
$new = $prefix.$letter.$num.".tif";
52
}
53
$new;
54
}
55
56
print "First input file: ";
57
chop($first = <STDIN>);
58
print "String B: ";
59
chop($B = <STDIN>);
60
print "First (3-digit) number XYZ: ";
61
chop($XYZ = <STDIN>);
62
63
while (1) {
64
$newname = $B.$XYZ.".tif";
65
print "cp $first $newname\n";
66
system "cp $first $newname\n";
67
$first = increment($first);
68
while (!(-e $first)) {
69
$first = increment($first);
70
}
71
$XYZ++;
72
}
73
74
75
76
77