Sort command giving wrong output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort command giving wrong output
# 1  
Old 04-03-2009
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  
Old 04-03-2009
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 06:49 AM..
# 3  
Old 04-03-2009
Quote:
Originally Posted by usha rao
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  
Old 04-03-2009
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 06:49 AM.. Reason: s/\[\//[\//
# 5  
Old 04-05-2009
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  
Old 04-05-2009
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  
Old 04-06-2009
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.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

VxWorks RTC time giving wrong value at random times

I am seeing a scenario where in if the TIMEZONE environment variable value is set to nothing i.e. putenv "TIMEZONE=" the hardware clock is +1 to software clock.Pasted below the results displayed: -> envShow (global environment) 0: TSC_TIME_FROM_RESET=420150.971529 seconds 1:... (0 Replies)
Discussion started by: snehavb
0 Replies

2. Shell Programming and Scripting

If && command giving wrong output

Hi All, I am trying to run a script which will search for 2 strings(stopped,started) in a text file and echo an output depending on below condition -bash-3.2$ cat trial1.txt v ggg f -bash-3.2$ cat trial1.sh VAR9=` grep 'stopped' /tmp/trial1.txt` VAR10=` grep 'started'... (4 Replies)
Discussion started by: srkmish
4 Replies

3. Shell Programming and Scripting

Grep command showing wrong output in Linux

Hi All I am trying to run a script in linux wherein i have a command like this grep ^prmAttunityUser= djpHewr2XFMAttunitySetup_ae1_tmp djpHewr2XFMAttunitySetup_ae1_tmp is a temporary file in which the user value is stored but this command in the script returns me balnk value whereas it has a... (4 Replies)
Discussion started by: vee_789
4 Replies

4. Shell Programming and Scripting

Wrong output in find command

Hi guys - I am trying a small script to tell me if there is a file that exists less than 1k. It should report ERROR, otherwise the check is good. I wrote this script down, however it never runs in the if/then statement. It always returns the echo ERROR. MYSIZE=$(find /home/student/dir1... (8 Replies)
Discussion started by: DallasT
8 Replies

5. UNIX for Advanced & Expert Users

xbindkeys giving wrong mapping information

Hello, I'm having a problem with xbindkeys giving the wrong mapping information, hence I can't get it work at all when trying new mappings from this machine. From another computer, I have some definitions for xbindkeys (made with xbindkeys-config). These key codes work correctly on this... (0 Replies)
Discussion started by: Narnie
0 Replies

6. Emergency UNIX and Linux Support

getting wrong output with AWK command!!!

i have a file which gets appended with 9 records daily and the file keeps growing from then...i use to store the previous day files count in a variable called oldfilecount and current files count as newfilecount.my requirement is that i need to start processing only the new records from the... (3 Replies)
Discussion started by: ganesh_248
3 Replies

7. Shell Programming and Scripting

tr command giving wrong output

Hi All, i have a file which have many fields delimited by ,(comma) now i have to show only few fields and not all. the sample text file looks like this: TYPE=SERVICEEVENT, TIMESTAMP=05/06/2009 11:01:40 PM, HOST=sppwa634, APPLICATION=ASComp, FUNCTION=LimitsService, SOU... (8 Replies)
Discussion started by: usha rao
8 Replies

8. UNIX for Dummies Questions & Answers

AWK command giving wrong input

Hi all, I have a problem with qwk command. i have to check process status and for that i am using command prstat -mvL 1 1 and it gives me the entire output but when i use this command with awk like this: prstat -mvL 1 1 | awk -F" " '{print $1,$15}' to get first and 15th arguments. ... (3 Replies)
Discussion started by: usha rao
3 Replies

9. AIX

Shared memory giving wrong value

Hi , I am working on AIX 5.3 server.I have small program which stores the from database to a particaular shared memory.But while retreiving the valus from the same shared memory, i am getting wrong values. Please help..... (1 Reply)
Discussion started by: ajaysahoo
1 Replies

10. Shell Programming and Scripting

Script giving wrong results....

hi, I have this script which gives me the result... #! /usr/bin/sh set -x cd /home/managar a=1 while true do if then echo " File log.txt exists in this directory " exit 0 fi echo " File has not arrived yes..." sleep 3 let a=a+1 if then (1 Reply)
Discussion started by: mgirinath
1 Replies
Login or Register to Ask a Question