Sponsored Content
Full Discussion: perl sorting
Top Forums Shell Programming and Scripting perl sorting Post 302167731 by joeyg on Friday 15th of February 2008 11:06:32 AM
Old 02-15-2008
a unix command would be

>sort rawfile -t'|' -k1.21,1.30 -r
Carl________15_X_Way____A004________LX
Will________2_A_St______A003________LX
Benny_______5_Spring____A002________LX
Joe_________12_Main_St__A001________LX


and that ordered my file reverse by what is in positions 21-30.
I did this by:
(a) specifying the 'tab' or key flag as the '|' which does not exist; thus all is considered field 1
(b) said to sort on k1.21,1.30 which translated to field one positions 21 to 30
(c) and the -r put in reverse order

So in perl, I figure somehow I need to define that I want to use character positions 21-30 as the key field. Then give a command to sort on those positions.
Perhaps this needs to be its own subroutine. I do not know.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: sorting files by whats in'em

i'm halfway into my script and realize i may need to use an associative array (a hash, i guess they are called in Perl). i'm fairly new to Perl and don't know how to use them correctly. i have some files in a directory. each file contains a number in a specific place in it's header. what i would... (4 Replies)
Discussion started by: quantumechanix
4 Replies

2. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

3. Shell Programming and Scripting

Perl: sorting by string

I have an array full of string values that need to be sorted, but if a value starts with (regex) 0^ it should be at the beginning of the array. Otherwise the array should be sorted normally using ascii sort. Please help me create the sub to pass to the sort function. (7 Replies)
Discussion started by: dangral
7 Replies

4. Shell Programming and Scripting

perl array sorting

Hi All, I have an array in perl as @match = (201001,201002,201001,201002); I am trying to sort this array as @match = sort(@match); print "@match"; I dont see the output sorted any answers I also tried another way, but still the results are not sorted foreach my $match (sort { $a... (2 Replies)
Discussion started by: bsdeepu
2 Replies

5. Shell Programming and Scripting

Perl sorting

Hi, I have a file in this format: a b c d e a b c d e a b c d e i need perl script to sort 2nd column in alphabatical order The script i use is #!/usr/bin/perl my @words=<>; foreach(sort mysort @words) { print; (4 Replies)
Discussion started by: Learnerabc
4 Replies

6. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

7. Shell Programming and Scripting

Sorting a particular column in PERL

I have a file abc.txt which contains data in th following format abc,23 hgfh,87 tweg,89 jdfjn,74 I want to sort on the basis of column (the second one). It should be numerical sort. output should be abc,23 jdfjn,74 hgfh,87 tweg,89 I know how to do it in unix. I need a PERL code (1 Reply)
Discussion started by: centurion_13
1 Replies

8. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

10. Shell Programming and Scripting

Perl Sorting Help

Hey guys, I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself. Basically I,m having my perl script run through a text pad and give the output in a special way e.g input deviceconfig { ... (5 Replies)
Discussion started by: zee3b
5 Replies
SORT(1) 						      General Commands Manual							   SORT(1)

NAME
sort - sort lines of text files SYNOPSIS
sort [-cmus] [-t separator] [-o output-file] [-T tempdir] [-bdfiMnr] [+POS1 [-POS2]] [-k POS1[,POS2]] [file...] sort {--help,--version} DESCRIPTION
This manual page documents the GNU version of sort. sort sorts, merges, or compares all the lines from the given files, or the standard input if no files are given. A file name of `-' means standard input. By default, sort writes the results to the standard output. sort has three modes of operation: sort (the default), merge, and check for sortedness. The following options change the operation mode: -c Check whether the given files are already sorted: if they are not all sorted, print an error message and exit with a status of 1. -m Merge the given files by sorting them as a group. Each input file should already be individually sorted. It always works to sort instead of merge; merging is provided because it is faster, in the case where it works. A pair of lines is compared as follows: if any key fields have been specified, sort compares each pair of fields, in the order specified on the command line, according to the associated ordering options, until a difference is found or no fields are left. If any of the global options Mbdfinr are given but no key fields are specified, sort compares the entire lines according to the global options. Finally, as a last resort when all keys compare equal (or if no ordering options were specified at all), sort compares the lines byte by byte in machine collating sequence. The last resort comparison honors the -r global option. The -s (stable) option disables this last- resort comparison so that lines in which all fields compare equal are left in their original relative order. If no fields or global options are specified, -s has no effect. GNU sort has no limits on input line length or restrictions on bytes allowed within lines. In addition, if the final byte of an input file is not a newline, GNU sort silently supplies one. If the environment variable TMPDIR is set, sort uses it as the directory in which to put temporary files instead of the default, /tmp. The -T tempdir option is another way to select the directory for temporary files; it overrides the environment variable. The following options affect the ordering of output lines. They may be specified globally or as part of a specific key field. If no key fields are specified, global options apply to comparison of entire lines; otherwise the global options are inherited by key fields that do not specify any special options of their own. -b Ignore leading blanks when finding sort keys in each line. -d Sort in `phone directory' order: ignore all characters except letters, digits and blanks when sorting. -f Fold lower case characters into the equivalent upper case characters when sorting so that, for example, `b' is sorted the same way `B' is. -i Ignore characters outside the ASCII range 040-0176 octal (inclusive) when sorting. -M An initial string, consisting of any amount of white space, followed by three letters abbreviating a month name, is folded to UPPER case and compared in the order `JAN' < `FEB' < ... < `DEC.' Invalid names compare low to valid names. -n Compare according to arithmetic value an initial numeric string consisting of optional white space, an optional - sign, and zero or more digits, optionally followed by a decimal point and zero or more digits. -r Reverse the result of comparison, so that lines with greater key values appear earlier in the output instead of later. Other options are: -o output-file Write output to output-file instead of to the standard output. If output-file is one of the input files, sort copies it to a tempo- rary file before sorting and writing the output to output-file. -t separator Use character separator as the field separator when finding the sort keys in each line. By default, fields are separated by the empty string between a non-whitespace character and a whitespace character. That is to say, given the input line ` foo bar', sort breaks it into fields ` foo' and ` bar'. The field separator is not considered to be part of either the field preceding or the field following it. -u For the default case or the -m option, only output the first of a sequence of lines that compare equal. For the -c option, check that no pair of consecutive lines compares equal. +POS1 [-POS2] Specify a field within each line to use as a sorting key. The field consists of the portion of the line starting at POS1 and up to (but not including) POS2 (or to the end of the line if POS2 is not given). The fields and character positions are numbered starting with 0. -k POS1[,POS2] An alternate syntax for specifying sorting keys. The fields and character positions are numbered starting with 1. A position has the form f.c, where f is the number of the field to use and c is the number of the first character from the beginning of the field (for +pos) or from the end of the previous field (for -pos). The .c part of a position may be omitted in which case it is taken to be the first character in the field. If the -b option has been given, the .c part of a field specification is counted from the first non- blank character of the field (for +pos) or from the first nonblank character following the previous field (for -pos). A +pos or -pos argument may also have any of the option letters Mbdfinr appended to it, in which case the global ordering options are not used for that particular field. The -b option may be independently attached to either or both of the +pos and -pos parts of a field speci- fication, and if it is inherited from the global options it will be attached to both. If a -n or -M option is used, thus implying a -b option, the -b option is taken to apply to both the +pos and the -pos parts of a key specification. Keys may span multiple fields. In addition, when GNU sort is invoked with exactly one argument, the following options are recognized: --help Print a usage message on standard output and exit successfully. --version Print version information on standard output then exit successfully. COMPATIBILITY
Historical (BSD and System V) implementations of sort have differed in their interpretation of some options, particularly -b, -f, and -n. GNU sort follows the POSIX behavior, which is usually (but not always!) like the System V behavior. According to POSIX -n no longer implies -b. For consistency, -M has been changed in the same way. This may affect the meaning of character positions in field specifica- tions in obscure cases. If this bites you the fix is to add an explicit -b. BUGS
The different meaning of field numbers depending on whether -k is used is confusing. It's all POSIX's fault! FSF
GNU Text Utilities SORT(1)
All times are GMT -4. The time now is 02:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy