The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK command giving wrong input usha rao UNIX for Dummies Questions & Answers 3 04-01-2009 04:41 AM
Shared memory giving wrong value ajaysahoo AIX 1 12-29-2008 06:42 AM
What's going wrong with sort? Digby Shell Programming and Scripting 7 09-03-2008 06:31 AM
script not giving the desired output Renjesh Shell Programming and Scripting 2 06-15-2008 03:58 AM
Script giving wrong results.... mgirinath Shell Programming and Scripting 1 12-21-2005 09:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-03-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
Question Sort command giving wrong output

Hi all,
I have a problem with sort command.
i have a file which looks like this:

Code:
 
"file1
1073 java/4
1073 java/180
1073 java/170
1073 java/176
1073 java/167
1073 java/40
1073 java/33
1073 java/136
28988 java/76
28988 java/73
28988 java/48
28988 java/26"
and i want to sort this file according to the number which comes after java/

so i am using this command:

Code:
 
"cat file1 | awk -F"/" '{print $2}' | sort "
but the output i am getting is
Code:
 
"136
167
170
176
180
26
33
4
40
48
73
76"
and as you can see it is not at all sorted.
Please advice/Thanks
  #2 (permalink)  
Old 04-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
 nawk -F/ '{printf("%s\t%s\n", $NF, $0)}' file1 | sort -k1n | cut -f2-

# or simply

sort -t/ -k2n file1

Last edited by vgersh99; 04-03-2009 at 05:49 AM..
  #3 (permalink)  
Old 04-03-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,839
Quote:
Originally Posted by usha rao View Post
Code:
 
136
167
170
176
180
26
33
4
40
48
73
76"
and as you can see it is not at all sorted.
Yes, it is. 136 has alphabetical precedence to 167. Same for 167 and 170, 170 and 176, ... If you want to have it sorted on numerical value instead of character order, use the -n switch
  #4 (permalink)  
Old 04-03-2009
Beast Of Bodmin Beast Of Bodmin is offline
Registered User
  
 

Join Date: Apr 2009
Location: Zurich
Posts: 9
Code:
sort -t/ -k2n file1
1073 java/4
28988 java/26
1073 java/33
1073 java/40
28988 java/48
28988 java/73
28988 java/76
1073 java/136
1073 java/167
1073 java/170
1073 java/176
1073 java/180
Code:
cut -d/ -f2 file1 | sort -n
4
26
33
40
48
73
76
136
167
170
176
180

Last edited by Beast Of Bodmin; 04-03-2009 at 05:49 AM.. Reason: s/\[\//[\//
  #5 (permalink)  
Old 04-05-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
Hi All,
Thanks for your replies.
It works.
Just one doubt if the same thing i want to sort in reverse order,will that be possible.
  #6 (permalink)  
Old 04-05-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,839
After invoking the Names of Cthulhu and Yog-Sothoth, asking for their guidance, a voice from the darkest reaches of the Netherworld told me: "Try sort --help". And lo, there was a bright text saying
Code:
  -r, --reverse               reverse the result of comparisons
  #7 (permalink)  
Old 04-06-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
Hey thanks pludi.I tried it earlier,may be my syntax was wrong.It gave me command garbled.But now i corrected it.Its working now.Thanks all for your help.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0