LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2008, 09:19 PM   #1
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Rep: Reputation: 0
awk sort function not sorting from lowest to highest.


I am trying to use the awk sort function to sort numbers from a column of numbers from lowest to highest.

Here is an example of what the numbers from the column look like:

1
2
3
10
4
12


I am using the following to sort the numbers:

awk '(print $1}' file | sort

The output is:

1
10
12
2
3
4

Clearly this is not the order of the numbers from lowest to highest. Can anyone give me advice on what has to added for the numbers to be sorted correctly.
 
Old 08-06-2008, 09:23 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The sort function you are using is not part of awk. It is a standard *nix command. The pipeline takes the output of the left hand side (awk), and sends it ot the right hand side (sort).

Try the -n option.

Don't forget to check out those man pages!

man sort
 
Old 08-06-2008, 09:29 PM   #3
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Original Poster
Rep: Reputation: 0
thanks for that

i'll be sure to check the man pages next.
 
Old 08-06-2008, 09:50 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have GNU awk
Code:
awk '{a[++d]=$1}
END{
 n = asort(a,b)
 for (i = 1; i <= n; i++) {
  print b[i]
 }
}' file
 
Old 08-09-2008, 12:20 AM   #5
haydar68
Member
 
Registered: Jul 2008
Posts: 35

Rep: Reputation: 15
Hi Skuz_Ball,

Here is a simple command:

awk -F ":" '{print|"sort -n"}' file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
....How to use awk to sort.... nisson Linux - General 12 11-02-2012 03:51 PM
External Merge Sort - in memory sorting sachitha Programming 0 10-15-2006 12:11 PM
sed/awk sort help Kvetch Programming 17 08-30-2006 07:21 PM
sorting text file - sort command man_linux Linux - General 16 08-09-2006 04:58 PM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration