sorting left-justified numeric values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting left-justified numeric values
Prev   Next
# 1  
Old 10-22-2012
Question sorting left-justified numeric values

I have a file which looks roughly like this:

996 mmmmmmm
996 xxxxxxxxxxxxx
99600 ssssssssss
9964 fffffffffffff

and would like to sort it numerically on the first field. I tried:

sort -nr --key=1 ....

The output I get is:

99600 ssssssssss
9964 fffffffffffff
996 mmmmmmm
996 xxxxxxxxxxxxx

The output I would like to have:

996 mmmmmmm
996 xxxxxxxxxxxxx
9964 fffffffffffff
99600 ssssssssss

Of course I can solve this by writing a one-liner in, say, Perl or Ruby, but I wonder why my approach had not worked, and how I can use the genuine 'sort' command to achieve my goal.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

Sorting file based on a numeric column

Hi, I use UBUNTU 12.04. I have a file with this structure: Name 2 1245787 A G 12 14 12 14 .... Name 1 1245789 C T 13 12 12 12..... I would like to sort my file based on the second column so to have this output for example: Name 1 1245789 C T 13 12 12 12..... Name 2 1245787 A G 12 14... (4 Replies)
Discussion started by: Homa
4 Replies

3. Shell Programming and Scripting

Sorting the alpha numeric results of Hash

Hi All I've got a perl script that I'm having a problem with when it prints the output of a hash. Some background. I'm trying to merge two file with a similar structure but with different data. Here is a portion of the script I'm using. while (<INPUT>) { my... (0 Replies)
Discussion started by: kingpin2502
0 Replies

4. Shell Programming and Scripting

sorting numeric array

Hi, I would like to do the following sorting, but the output is not what i expected. Why 222 and 2222 are not at the last two elements of array? awk 'BEGIN{a="22";a="2222";a="33";a="44";a="222";a="11";a="22";a="33";asort(a); for (i=1;i<=8;i++) print a}' 11 22 22 222 2222 33 33 44... (1 Reply)
Discussion started by: phoeberunner
1 Replies

5. Shell Programming and Scripting

Sorting the data right justified with through awk.

Dear all, I have a inputfile like some what like this - 1000.98651 1000.96696 999.98904 991.66864 986.51829 986.49467 17.44122 16.74039 16.74021 10.92725 desired output 1000.98651 1000.96696 0999.98904 (4 Replies)
Discussion started by: admax
4 Replies

6. Shell Programming and Scripting

How to check if the file contains only numeric values

How to check if the file contains only numeric values. I don't want to read entire file it eats lot of cpu Or any way which consumes less memory n cpu.. Please suggest -S (2 Replies)
Discussion started by: sunilmenhdiratt
2 Replies

7. Programming

numeric values ending in 'U'

I am getting back on the C++ programming after many years away. I recently received an SDK that has code like this where numeric values end in 'U'. What does this mean? if ((ptr % 16U) == 0U) return buffer; (3 Replies)
Discussion started by: sneakyimp
3 Replies

8. Shell Programming and Scripting

Remove non numeric values from a variable

Hello all, I am working on a basic script but need a little help. Issue: I am running a SQL Query using sqlplus and a shell script. I have the output of the statement stored as variable $A. $A is set to "other text here 45678754 other text here". I need to strip all text except that numeric... (13 Replies)
Discussion started by: ownedthawte
13 Replies

9. Shell Programming and Scripting

stripping out non-numeric values in a list

hi all, i'm very new to scripting and have the folllowing issue. I have used a few commands to get a list of numbers, but I need to strip away the non-numeric ones, and then need a total of all values. any ideas? root@unixserver # cat myfile | awk '{print $8}'| sort -rn 1504 1344 896 704... (2 Replies)
Discussion started by: badoshi
2 Replies

10. Shell Programming and Scripting

Sorting with multiple numeric keys

Data I want to sort :- 1 10 jj Y 2 100 vv B 19 5 jj A 1 11 hq D 3 8 op X 44 78 ds GG 1 8 hq D and want to sort based on the first 2 columns - which hold numeric values. Am using : cat filename | sort -nk 1,2 But the result is :- 1 10 jj Y 1 11 hq D (1 Reply)
Discussion started by: sinpeak
1 Replies
Login or Register to Ask a Question
Sort::Naturally(3pm)					User Contributed Perl Documentation				      Sort::Naturally(3pm)

NAME
Sort::Naturally -- sort lexically, but sort numeral parts numerically SYNOPSIS
@them = nsort(qw( foo12a foo12z foo13a foo 14 9x foo12 fooa foolio Foolio Foo12a )); print join(' ', @them), " "; Prints: 9x 14 foo fooa foolio Foolio foo12 foo12a Foo12a foo12z foo13a (Or "foo12a" + "Foo12a" and "foolio" + "Foolio" and might be switched, depending on your locale.) DESCRIPTION
This module exports two functions, "nsort" and "ncmp"; they are used in implementing my idea of a "natural sorting" algorithm. Under natural sorting, numeric substrings are compared numerically, and other word-characters are compared lexically. This is the way I define natural sorting: o Non-numeric word-character substrings are sorted lexically, case-insensitively: "Foo" comes between "fish" and "fowl". o Numeric substrings are sorted numerically: "100" comes after "20", not before. o W substrings (neither words-characters nor digits) are ignored. o Our use of w, d, D, and W is locale-sensitive: Sort::Naturally uses a "use locale" statement. o When comparing two strings, where a numeric substring in one place is not up against a numeric substring in another, the non-numeric always comes first. This is fudged by reading pretending that the lack of a number substring has the value -1, like so: foo => "foo", -1 foobar => "foo", -1, "bar" foo13 => "foo", 13, foo13xyz => "foo", 13, "xyz" That's so that "foo" will come before "foo13", which will come before "foobar". o The start of a string is exceptional: leading non-W (non-word, non-digit) components are are ignored, and numbers come before letters. o I define "numeric substring" just as sequences matching m/d+/ -- scientific notation, commas, decimals, etc., are not seen. If your data has thousands separators in numbers ("20,000 Leagues Under The Sea" or "20.000 lieues sous les mers"), consider stripping them before feeding them to "nsort" or "ncmp". The nsort function This function takes a list of strings, and returns a copy of the list, sorted. This is what most people will want to use: @stuff = nsort(...list...); When nsort needs to compare non-numeric substrings, it uses Perl's "lc" function in scope of a <use locale>. And when nsort needs to lowercase things, it uses Perl's "lc" function in scope of a <use locale>. If you want nsort to use other functions instead, you can specify them in an arrayref as the first argument to nsort: @stuff = nsort( [ &string_comparator, # optional &lowercaser_function # optional ], ...list... ); If you want to specify a string comparator but no lowercaser, then the options list is "[&comparator, '']" or "[&comparator]". If you want to specify no string comparator but a lowercaser, then the options list is "['', &lowercaser]". Any comparator you specify is called as "$comparator->($left, $right)", and, like a normal Perl "cmp" replacement, must return -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument. Any lowercaser function you specify is called as "$lowercased = $lowercaser->($original)". The routine must not modify its $_[0]. The ncmp function Often, when sorting non-string values like this: @objects_sorted = sort { $a->tag cmp $b->tag } @objects; ...or even in a Schwartzian transform, like this: @strings = map $_->[0] sort { $a->[1] cmp $b->[1] } map { [$_, make_a_sort_key_from($_) ] @_ ; ...you wight want something that replaces not "sort", but "cmp". That's what Sort::Naturally's "ncmp" function is for. Call it with the syntax "ncmp($left,$right)" instead of "$left cmp $right", but otherwise it's a fine replacement: @objects_sorted = sort { ncmp($a->tag,$b->tag) } @objects; @strings = map $_->[0] sort { ncmp($a->[1], $b->[1]) } map { [$_, make_a_sort_key_from($_) ] @_ ; Just as with "nsort" can take different a string-comparator and/or lowercaser, you can do the same with "ncmp", by passing an arrayref as the first argument: ncmp( [ &string_comparator, # optional &lowercaser_function # optional ], $left, $right ) You might get string comparators from Sort::ArbBiLex. NOTES
o This module is not a substitute for Sort::Versions! If you just need proper version sorting, use that! o If you need something that works sort of like this module's functions, but not quite the same, consider scouting thru this module's source code, and adapting what you see. Besides the functions that actually compile in this module, after the POD, there's several alternate attempts of mine at natural sorting routines, which are not compiled as part of the module, but which you might find useful. They should all be working implementations of slightly different algorithms (all of them based on Martin Pool's "nsort") which I eventually discarded in favor of my algorithm. If you are having to naturally-sort very large data sets, and sorting is getting ridiculously slow, you might consider trying one of those discarded functions -- I have a feeling they might be faster on large data sets. Benchmark them on your data and see. (Unless you need the speed, don't bother. Hint: substitute "sort" for "nsort" in your code, and unless your program speeds up drastically, it's not the sorting that's slowing things down. But if it is "nsort" that's slowing things down, consider just: if(@set >= SOME_VERY_BIG_NUMBER) { no locale; # vroom vroom @sorted = sort(@set); # feh, good enough } elsif(@set >= SOME_BIG_NUMBER) { use locale; @sorted = sort(@set); # feh, good enough } else { # but keep it pretty for normal cases @sorted = nsort(@set); } o If you do adapt the routines in this module, email me; I'd just be interested in hearing about it. o Thanks to the EFNet #perl people for encouraging this module, especially magister and a-mused. COPYRIGHT AND DISCLAIMER
Copyright 2001, Sean M. Burke "sburke@cpan.org", all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. AUTHOR
Sean M. Burke "sburke@cpan.org" perl v5.10.0 2004-12-30 Sort::Naturally(3pm)