Sort(3pm) | User Contributed Perl Documentation | Sort(3pm) |
File::Sort - Sort a file or merge sort multiple files
use File::Sort qw(sort_file); sort_file({ I => [qw(file_1 file_2)], o => 'file_new', k => '5.3,5.5rn', -t => '|' }); sort_file('file1', 'file1.sorted');
This module sorts text files by lines (or records). Comparisons are based on one or more sort keys extracted from each line of input, and are performed lexicographically. By default, if keys are not given, sort regards each input line as a single field. The sort is a merge sort. If you don't like that, feel free to change it.
The following options are available, and are passed in the hash reference passed to the function in the format:
OPTION => VALUE
Where an option can take multiple values (like "I", "k", and "pos"), values may be passed via an anonymous array:
OPTION => [VALUE1, VALUE2]
Where the OPTION is a switch, it should be passed a boolean VALUE of 1 or 0.
This interface will always be supported, though a more perlish interface may be offered in the future, as well. This interface is basically a mapping of the command-line options to the Unix sort utility.
The following options override the default ordering rules. When ordering options appear independent of any key field specifications, the requested field ordering rules are applied globally to all sort keys. When attached to a specific key (see k), the specified ordering options override all global ordering options for that key.
The string matched by STRING is not included in the fields themselves, unless demanded by perl's regex and split semantics (e.g., regexes in parentheses will add that matched expression as an extra field). See perlre and "split" in perlfunc.
field_start[.first_char][type][,field_end[.last_char][type]]
where field_start and field_end define a key field restricted to a portion of the line, and type is a modifier from the list of characters b, d, f, i, n, r. The b modifier behaves like the b option, but applies only to the field_start or field_end to which it is attached. The other modifiers behave like the corresponding options, but apply only to the key field to which they are attached; they have this effect if specified with field_start, field_end, or both. If any modifier is attached to a field_start or a field_end, no option applies to either.
Occurrences of the k option are significant in command line order. If no k option is specified, a default sort key of the entire line is used. When there are multiple keys fields, later keys are compared only after all earlier keys compare equal.
Except when the u option is specified, lines that otherwise compare equal are ordered as if none of the options d, f, i, n or k were present (but with r still in effect, if it was specified) and with all bytes in the lines significant to the comparison. The order in which lines that still compare equal are written is unspecified.
+field_start[.first_char][type] [-field_end[.last_char][type]]
Where field_end in k specified the last position to be included, it specifes the last position to NOT be included. Also, numbers are counted from 0 instead of 1. pos2 must immediately follow corresponding +pos1. The rest should be the same as the k option.
Mixing +pos1 pos2 with k is allowed, but will result in all of the +pos1 pos2 options being ordered AFTER the k options. It is best if you Don't Do That. Pick one and stick with it.
Here are some equivalencies:
pos => '+1 -2' -> k => '2,2' pos => '+1.1 -1.2' -> k => '2.2,2.2' pos => ['+1 -2', '+3 -5'] -> k => ['2,2', '4,5'] pos => ['+2', '+0b -1'] -> k => ['3', '1b,1'] pos => '+2.1 -2.4' -> k => '3.2,3.4' pos => '+2.0 -3.0' -> k => '3.1,4.0'
If the options are not listed as implemented above, or are not listed in TODO below, they are not in the plan for implementation. This includes T and z.
Sort file by straight string compare of each line, sending output to STDOUT.
use File::Sort qw(sort_file); sort_file('file');
Sort contents of file by second key in file.
sort_file({k => 2, I => 'file'});
Sort, in reverse order, contents of file1 and file2, placing output in outfile and using second character of second field as the sort key.
sort_file({ r => 1, k => '2.2,2.2', o => 'outfile', I => ['file1', 'file2'] });
Same sort but sorting numerically on characters 3 through 5 of the fifth field first, and only return records with unique keys.
sort_file({ u => 1, r => 1, k => ['5.3,5.5rn', '2.2,2.2'], o => 'outfile', I => ['file1', 'file2'] });
Print passwd(4) file sorted by numeric user ID.
sort_file({t => ':', k => '3n', I => '/etc/passwd'});
For the anal sysadmin, check that passwd(4) file is sorted by numeric user ID.
sort_file({c => 1, t => ':', k => '3n', I => '/etc/passwd'});
Note that if you change the locale settings after the program has started up, you must call setlocale() for the new settings to take effect. For example:
# get constants use POSIX 'locale_h'; # e.g., blank out locale $ENV{LC_ALL} = $ENV{LANG} = ''; # use new ENV settings setlocale(LC_CTYPE, ''); setlocale(LC_COLLATE, '');
Exports "sort_file" on request.
Add O_TRUNC to output open (D'oh!).
Played with somem of the -k options (Marco A. Romero).
Fix filehandle close test of STDOUT (Gael Marziou).
Some cleanup.
Documented "I" option. (Hubert Toullec)
Removed O_EXCL flag from "sort_file".
Fixed bug in sorting multiple files. (Paul Eckert)
Switched to sysopen for better portability.
Print to STDOUT if no output file supplied.
Added c option to check sorting.
Fixed up docs and did some more tests and benchmarks.
Also now use "IO::File" to create temp files, so the TMPDIR option is no longer supported. Hopefully made the whole thing more robust and faster, while supporting more options for sorting, including delimited sorts, and arbitrary sorts.
Made CHUNK default a lot larger, which improves performance. On low-memory systems, or where (e.g.) the MacPerl binary is not allocated much RAM, it might need to be lowered.
Mike Blazer <blazer@mail.nevalink.ru>, Vicki Brown <vlb@cfcl.com>, Tom Christiansen <tchrist@perl.com>, Albert Dvornik <bert@mit.edu>, Paul Eckert <peckert@epicrealm.com>, Gene Hsu <gene@moreinfo.com>, Andrew M. Langmead <aml@world.std.com>, Gael Marziou <gael_marziou@hp.com>, Brian L. Matthews <blm@halcyon.com>, Rich Morin <rdm@cfcl.com>, Matthias Neeracher <neeri@iis.ee.ethz.ch>, Miko O'Sullivan <miko@idocs.com>, Tom Phoneix <rootbeer@teleport.com>, Marco A. Romero <mromero@iglou.com>, Gurusamy Sarathy <gsar@activestate.com>, Hubert Toullec <Hubert.Toullec@wanadoo.fr>.
Chris Nandor <pudge@pobox.com>, http://pudge.net/
Copyright (c) 1997-2002 Chris Nandor. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
v1.01, Monday, January 14, 2002
sort(1), locale, PPT project, <URL:http://sf.net/projects/ppt/>.
2022-12-06 | perl v5.36.0 |