7 td-sort - Sort tabular data by the columns given by name
15 All those which are accepted by sort(1),
16 except you don't need to refer to columns by ordinal number,
21 =item -k, --key=I<KEYDEF>
23 sort(1) defines I<KEYDEF> as C<F[.C][OPTS][,F[.C][OPTS]]>,
24 where B<F> is the (1-based) field number.
25 However with td-sort(1) you may refer to fields by name.
26 But since B<F> is no longer consists only of digits,
27 but is an arbitrary string,
28 it's may be ambiguous where the name ends.
29 So you may enclose them in round/square/curly/angle brackets.
30 Choose the one which does not occur in the column name.
32 You don't need to even type B<-k>, because a lone I<COLUMN-NAME>
33 is interpreted as "B<-k> I<F>" where I<F> is the corresponding field number.
39 td-sort(1) is analogous to SQL ORDER BY.
43 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
44 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
48 sub replace_colnames_in_string
51 $s =~ s/(\((?'name'[^)]+)\)|\[(?'name'[^]]+)\]|\{(?'name'[^}]+)\}|<(?'name'[^>]+)>|(?'name'[^.,]+))(?'rest'(\.\d+)?[^,]*)/replace_colname($+{'name'}).$+{'rest'}/eg;
58 my $colnum = $Header{$name};
59 if(not defined $colnum)
61 my $cols = join ", ", @Header;
62 die "$0: $name: no such column. known columns: $cols\n"
69 $Header = sys_read_line
();
70 process_header
($Header);
73 @sort_args = ('--field-separator='.$FS,);
78 my $arg = shift @ARGV;
80 if($arg =~ /^(--key=)(.+)$/)
82 $arg = $1.replace_colnames_in_string
($2);
84 elsif($prev_arg ~~ ['-k', '--key'])
86 $arg = replace_colnames_in_string
($arg);
90 $arg = '--key=' . replace_colnames_in_string
($arg);
93 push @sort_args, $arg;
100 exec 'sort', @sort_args;
101 ($errno, $errstr) = (int $!, $!);
102 warn "sort: $errstr\n";