sorting left-justified numeric values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting left-justified numeric values
# 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.
# 2  
Old 10-22-2012
Drop the -r option.
Code:
sort -n --key=1 ....

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-22-2012
Quote:
Originally Posted by elixir_sinari
Drop the -r option.
Arrrrrrrrrrrgh Smilie

Thanks a lot, I think it's time for going home.......

Ronald
# 4  
Old 10-22-2012
Unless you are intentionally restricting the portability of your code, it's unwise to use GNU long options when there exists a traditional, ubiquitous, and equivalent short option. sort -n -k1 will work just about anywhere (Linux, Solaris, AIX, HP-UX, OS X, FreeBSD, NetBSD, OpenBSD, et al). While sort -n --key=1 is unlikely to work beyond GNU/Linux.

If portability is not a concern, please disregard this post.

Regards,
Alister
This User Gave Thanks to alister For This Post:
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