Debian Codegraph @

wtools/rsort

raw
1 #!/usr/bin/perl
2 #######################################################
3 #
4 # Written by Maria Lantin ([email protected]), 1996/11/01
5 #
6 # rsort was written to sort ace dump but
7 # is general enough to be used for
8 # other purposes.
9 #
10 # Bug reports and suggestions should be sent to
11 # [email protected]
12 #
13 # Added to ACEDB wtools/ directory by RD 961108
14 #
15 # $Id: rsort,v 1.1 1996/12/02 00:50:23 rd Exp $
16 #######################################################
17
18 use FileHandle;
19
20 ProcessOptions();
21
22 # Set the record seperator to be an empty line
23 # and read the file into a 2-dimensional array where
24 # each column is a field (line) of the record.
25 $infile->input_record_separator("");
26 $recnums = 0;
27 while (<$infile>) {
28 push @records, [ split(/\n/) ];
29 }
30
31 # Sort the the records by each sort field specified in the
32 # -f options. If no fields are specified sortfields = "1"
33 for ($i = $#sortfields; $i >= 0; $i--) {
34 $sortfield = $sortfields[$i];
35 @records = sort byfield @records;
36 }
37
38 # reverse the order of the records if the '-d' option was specified
39 if ($opt_d) {
40 @records = reverse @records;
41 }
42
43 # output the records
44 $outfile->output_field_separator("\n");
45 $outfile->output_record_separator("\n\n");
46 foreach $record ( @records ) {
47 print $outfile @$record;
48 }
49
50 # Sorting routine used by the "sort" command above. It is passed two
51 # records $a and $b and it must return -1 if $a < $b, +1 if $a > $b, or
52 # 0 if $a == $b
53 sub byfield {
54 local($value1, $value2, $val1, $val2, $field, $sortf, $format);
55
56 # A sortfield is of the form field[a] or field[n]. The first
57 # form says to sort on field alphanumerically while the second
58 # says to sort on field numerically. The default is [a] if
59 # it's not specified. This next line parses out the field and
60 # the type of sort (alphanumeric or numeric).
61 $sortfield =~ /([^\[]*)(\[([an])\])?/, $sortf = $1, $format = $3;
62
63 # if we didn't get a format, assume 'a'
64 if (!$format) {
65 $format = "a";
66 }
67
68 # if we're simply sorting on a field number then make value1 be
69 # the field we're sorting on for $a and value2 be the corresponding
70 # field for $b. It is assumed that the first field is 1.
71 if ($sortf =~ /^[0-9]+$/) { # The sort field is numeric
72 $value1 = $$a[$sortf-1];
73 $value2 = $$b[$sortf-1];
74 } else {
75 # the sort field is a string so we assume that the field name will
76 # be the first string on the line. We parse for it and make value1
77 # be the rest of the line after the field name for $a and
78 # value2 be the corresponding value for $b
79 foreach $field (@$a) {
80 $field =~ /^\s*$sortf\s+(.*)/ && ($value1 = $1, last) ;
81 }
82 foreach $field (@$b) {
83 $field =~ /^\s*$sortf\s+(.*)/ && ($value2 = $1, last) ;
84 }
85 }
86
87 if ($format eq "n") {
88 # We are sorting numerically so we must do our best to find a number
89 # in the values recorded in the above statements. val1 and val2
90 # become the numbers we will be comparing for $a and $b
91 # respectively. If there are 2 numbers on the field, only the
92 # first one will be picked out.
93 $value1 =~ /(\d+)/ && ($val1 = $1);
94 $value2 =~ /(\d+)/ && ($val2 = $1);
95 $val1 <=> $val2;
96 } else {
97 # We are comparing alphanumerically so convert everything to
98 # lowercase and compare the strings.
99 lc($value1) cmp lc($value2);
100 }
101 }
102
103 sub Usage {
104 # Prints out the usage string for this utility
105
106 print "This utility sorts multiline records\n";
107 print "Usage: rsort [-f fieldlist] [-o outfile] [-v] [-h] [-d] [infile]\n";
108 print "Options:\n";
109 print "\t-d sorts the records in descending order\n";
110 print "\t-f fieldlist sorts the record by the comma separated field list.\n";
111 print "\t A field consists of a field name or number\n";
112 print "\t followed by an optional format specifier.\n";
113 print "\t A format specifier is one of [a] or [n] where\n";
114 print "\t the first is used to sort alphanumerically (default)\n";
115 print "\t and the second is used to sort numerically.\n";
116 print "\t-o file puts sorted record into outfile\n";
117 print "\t-v -h print this message and exit\n\n";
118 print "Examples:\n";
119 print "\trsort -fEmbryo_division_time[n] -o sortedcells.ace cell.ace\n";
120 print "\t will sort cell.ace by Embryo_division_time numerically and\n";
121 print "\t will put the newly sorted records in sortedcells.ace\n";
122 print "\trsort -f1,Embryo_division_time[n] cell.ace\n";
123 print "\t will sort cell.ace by the first field and numerically by\n";
124 print "\t the Embryo_division_time field. The output will go\n";
125 print "\t to standard output.\n";
126 }
127
128 # Procedure to process the options given on the command line.
129 sub ProcessOptions {
130 use Getopt::Long;
131 GetOptions("v","h","d","f=s","o=s");
132
133 # help
134 if ($opt_v || $opt_h) {
135 Usage();
136 exit(1);
137 }
138 # output file
139 if ($opt_o) {
140 $outfile = new FileHandle($opt_o, "w");
141 } else {
142 $outfile = STDOUT;
143 }
144 # field list
145 if ($opt_f) {
146 @sortfields = split(/,/,$opt_f);
147 } else {
148 @sortfields = 1;
149 }
150
151 # input file
152 if ($#ARGV > 0) {
153 Usage();
154 } else {
155 if ($#ARGV == 0) {
156 $infile = new FileHandle $ARGV[0];
157 } else {
158 $infile = STDIN;
159 }
160 }
161 }
162