| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||
| 2 | 6 | eval 'exec perl -S $0 "$@"' | |||||||||||||||||
| 3 | if 0; | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 6 | ▶ | use vars qw( $running_under_some_shell ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | cssort -- Czech sort | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | =head1 FORMAT | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | cssort [ C<-c>B<list> | C<-f>B<list> [C<-d>B<regexp>]] [files ...] | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | cssort -c10-15,50-,25-45 < file | ||||||||||||||||||
| 19 | cssort -f3,5-6 < file | ||||||||||||||||||
| 20 | cssort -f3,5-6 -s: < file | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | Cssort is a utility that sorts input lines according to rules used in | ||||||||||||||||||
| 25 | the Czech language. You can run it without any options, then it just | ||||||||||||||||||
| 26 | uses whole lines for sorting. With the options, it's possible to | ||||||||||||||||||
| 27 | specify parts of the lines to be used for comparison. | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | =over 4 | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | =item B<list> | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | A comma-separated list of integer field numbers or field ranges. The | ||||||||||||||||||
| 34 | are indexed from 1 and if a range is open (eg. C<5->), it means all | ||||||||||||||||||
| 35 | remaining fields from the starting number. | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | =item B<-c> | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | Stands for columns and the list that follows specifies byte ranges on | ||||||||||||||||||
| 40 | the line. You will probably use this option to sort data with fixed | ||||||||||||||||||
| 41 | width fields. | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | =item B<-f> | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | Fields that will be used for sort. | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | =item B<-d> | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | Delimiter that separates fields in the B<-f> option. It is a Perl | ||||||||||||||||||
| 50 | regular expression, the default is C<[ \t]+>, which means any number | ||||||||||||||||||
| 51 | of spaces or tabs in a row. | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | =back | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | The program assumes ISO-8859-2 encoding. Some way to specify another | ||||||||||||||||||
| 56 | input encoding will come in the next versions. If you need to sort | ||||||||||||||||||
| 57 | files with different encodings, you might want to check the B<cstocs> | ||||||||||||||||||
| 58 | conversion utility. | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | =head1 SEE ALSO | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | Cz::Sort(3), cstocs(1). | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =head1 AUTHOR | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | Jan Pazdziora | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | =cut | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 6 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 71 | 6 | ▶ | use Getopt::Std; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 72 | 6 | ▶ | use Cz::Sort; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 6 | my %opts = ( | |||||||||||||||||
| 75 | 'd' => '[ \t]+', | ||||||||||||||||||
| 76 | ); | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 6 | getopt('dfce', \%opts); | |||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 6 | ▶ | if (defined $opts{'h'}) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 81 | { | ||||||||||||||||||
| 82 | 0 | print STDERR <<"EOF"; | |||||||||||||||||
| 83 | This is cssort version $Cz::Sort::VERSION. | ||||||||||||||||||
| 84 | Usage info: cssort [ -clist | -flist [-dregexp]] [files ...] | ||||||||||||||||||
| 85 | -c Columns | ||||||||||||||||||
| 86 | -f Field numbers | ||||||||||||||||||
| 87 | -d Delimiter, field separator | ||||||||||||||||||
| 88 | Lists are comma separated lists of field (column) numbers or ranges. | ||||||||||||||||||
| 89 | Example: cssort -c10-15,50-,25-45 cssort -f3,5-6 -s: | ||||||||||||||||||
| 90 | EOF | ||||||||||||||||||
| 91 | 0 | exit(1); | |||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 6 | my $switch = 'c'; | |||||||||||||||||
| 95 | 6 | my $option = $opts{$switch}; | |||||||||||||||||
| 96 | 6 | ▶ | if (not defined $option) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 97 | { | ||||||||||||||||||
| 98 | 6 | $switch = 'f'; | |||||||||||||||||
| 99 | 6 | $option = $opts{$switch}; | |||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | 6 | ▶ | if (not defined $option) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 102 | { | ||||||||||||||||||
| 103 | 6 | $switch = undef; | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 6 | my $conversion; | |||||||||||||||||
| 107 | 6 | ▶ | if (defined $opts{'e'}) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | { | ||||||||||||||||||
| 109 | 0 | require Cz::Cstocs; | |||||||||||||||||
| 110 | 0 | $conversion = new Cz::Cstocs $opts{'e'}, 'il2'; | |||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | 6 | ▶ | if (defined $switch) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 114 | { | ||||||||||||||||||
| 115 | 0 | my (@starts, @lengths, @array); | |||||||||||||||||
| 116 | 0 | for (split /,/, $option) | |||||||||||||||||
| 117 | { | ||||||||||||||||||
| 118 | 0 | ▶ | if (/^\d+$/) | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 119 | 0 | { push @starts, $_ - 1; push @lengths, 1; } | |||||||||||||||||
| 120 | elsif (/^(\d+)-(\d+)$/) | ||||||||||||||||||
| 121 | 0 | { push @starts, $1 - 1; push @lengths, ($2 - $1 + 1); } | |||||||||||||||||
| 122 | elsif (/^(\d+)-$/) | ||||||||||||||||||
| 123 | 0 | { push @starts, $1 - 1; push @lengths, undef; } | |||||||||||||||||
| 124 | else | ||||||||||||||||||
| 125 | 0 | { die "Cssort: wrong option '$_' for switch -$switch\n"; } | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 0 | ▶ | if ($switch eq 'c') | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 129 | { | ||||||||||||||||||
| 130 | 0 | ▶ | while (<>) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 131 | { | ||||||||||||||||||
| 132 | 0 | chomp; | |||||||||||||||||
| 133 | 0 | my $line = [ $_ ]; | |||||||||||||||||
| 134 | 0 | my $i; | |||||||||||||||||
| 135 | 0 | ▶ | for ($i = 0; $i < @starts; $i++) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 136 | { | ||||||||||||||||||
| 137 | 0 | ▶ | if ($starts[$i] >= length $_) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 138 | 0 | { push @$line, undef; } | |||||||||||||||||
| 139 | elsif (defined $lengths[$i]) | ||||||||||||||||||
| 140 | 0 | { push @$line, substr $_, $starts[$i], $lengths[$i]; } | |||||||||||||||||
| 141 | else | ||||||||||||||||||
| 142 | 0 | { push @$line, substr $_, $starts[$i]; } | |||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | 0 | push @array, $line; | |||||||||||||||||
| 145 | } | ||||||||||||||||||
| 146 | } | ||||||||||||||||||
| 147 | else | ||||||||||||||||||
| 148 | { | ||||||||||||||||||
| 149 | 0 | my $regexp = $opts{'d'}; | |||||||||||||||||
| 150 | 0 | ▶ | while (<>) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 151 | { | ||||||||||||||||||
| 152 | 0 | chomp; | |||||||||||||||||
| 153 | 0 | my @items = split /$regexp/so; | |||||||||||||||||
| 154 | 0 | my $line = [ $_ ]; | |||||||||||||||||
| 155 | 0 | my $i; | |||||||||||||||||
| 156 | 0 | ▶ | for ($i = 0; $i < @starts; $i++) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 157 | { | ||||||||||||||||||
| 158 | 0 | ▶ | push @$line, @items[$starts[$i] .. (defined $lengths[$i] ? $starts[$i] + $lengths[$i] - 1 : $#items )]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 159 | } | ||||||||||||||||||
| 160 | 0 | push @array, $line; | |||||||||||||||||
| 161 | } | ||||||||||||||||||
| 162 | } | ||||||||||||||||||
| 163 | |||||||||||||||||||
| 164 | 0 | print map { $_->[0] . "\n" } | |||||||||||||||||
| 165 | sort | ||||||||||||||||||
| 166 | { | ||||||||||||||||||
| 167 | 0 | ▶ | my $len = ( @$a >= @$b ? @$a : @$b); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 168 | 0 | my $i; | |||||||||||||||||
| 169 | 0 | ▶ | for ($i = 1; $i < $len; $i++) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 170 | { | ||||||||||||||||||
| 171 | 0 | ▶ | if (not defined $a->[$i]) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 172 | { | ||||||||||||||||||
| 173 | 0 | ▶ | return 0 if not defined $b->[$i]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 174 | 0 | return -1; | |||||||||||||||||
| 175 | } | ||||||||||||||||||
| 176 | 0 | ▶ | if (not defined $b->[$i]) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 177 | { | ||||||||||||||||||
| 178 | 0 | return 1; | |||||||||||||||||
| 179 | } | ||||||||||||||||||
| 180 | 0 | my $result = czcmp($a->[$i], $b->[$i]); | |||||||||||||||||
| 181 | 0 | ▶ | return $result if $result != 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 182 | } | ||||||||||||||||||
| 183 | 0 | return 0; | |||||||||||||||||
| 184 | } @array; | ||||||||||||||||||
| 185 | } | ||||||||||||||||||
| 186 | else | ||||||||||||||||||
| 187 | 6 | { print czsort <>; } | |||||||||||||||||
| 188 | |||||||||||||||||||
| 189 | |||||||||||||||||||