Issue with Sort in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with Sort in Unix
# 1  
Old 08-03-2011
Network Issue with Sort in Unix

Hi All,

I am trying to sort the below data using sort command.

temp.dat
Code:
H|S1-511091486889|27-Jul-2011 00:00:00
H|S1-511091486823|27-Jul-2011 00:00:00
H|S1-511091486757|27-Jul-2011 00:00:00
L|S1-511091486889|1
L|S1-511091486823|1
L|S1-511091486757|1

Code:
sort -t "|" -k2 -k1 temp.dat

My output is
Code:
L|S1-511091486757|1
H|S1-511091486757|27-Jul-2011 00:00:00
L|S1-511091486823|1
H|S1-511091486823|27-Jul-2011 00:00:00
L|S1-511091486889|1
H|S1-511091486889|27-Jul-2011 00:00:00

I am expecting "H" to appear first and "L" next. Can someone help me why this is soring in reverse way.

Thanks,
Deepak

Last edited by radoulov; 08-03-2011 at 05:50 AM.. Reason: Code tags.
# 2  
Old 08-03-2011
Because you're sorting by the second field first.

Try:

Code:
sort temp.dat

# 3  
Old 08-03-2011
Code:
sort -t'|' -k2,2 -k1,1  INPUTFILE

# 4  
Old 08-03-2011
"-k2 -k1" means:
first, compare values from second field to the end of line
second, if values are equal in the first comparison, then compare from the first field to the end of line
# 5  
Old 08-03-2011
To get ascending sort of the first field you need to reverse the direction of your sort
Code:
sort -t '|' -k2 -rk1 tmp.dat

# 6  
Old 08-08-2011
Hi All,

Thanks a lot for your time. It worked now.

Thanks,
Deepak
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX sort

Hi I have below pattern A: Apple 2 B:Bolls 4 total_count = 6 A: pens 4 B:Bags 4 total count = 8 A: pens 4 B:Bags 4 A: cells 6 A: jobs 6 Output I need : A: Apple 2 B:Bolls 4 total_count = 6 A: pens 4 B:Bags 4 total count = 8 A: cells 6 A: jobs 6 ... (7 Replies)
Discussion started by: pkkanduk
7 Replies

2. UNIX for Advanced & Expert Users

Sort command using unix

Hello, I have a file with 'tab' separated columns. The first column of the file contains integer values. Now I want to sort the first column in ascending order. Can someone help me on this... thanks in advance (1 Reply)
Discussion started by: koneru_18
1 Replies

3. Shell Programming and Scripting

Sort with UNIX

I want to sort unique values of column 2 that has the maximum value at column 7. this is my file and desired output I have a file like this: AD008 AD0081010180947 101018 0947 0950 1010180947 1010180950 AD008 AD0081010180947 101018 0947 0956 1010180947 1010180956 AD008 AD0081010180947... (12 Replies)
Discussion started by: aydj
12 Replies

4. Shell Programming and Scripting

Sort Issue

these are two records(adr.txt)trying to sort with the the code expl below. 5423|336110|2730 Pierce St|Ste 300|Sioux City|IA|Woodbury|51104|3765||42518651|96405013|A|2|3|U|12/08/2009 5423|14462335|624 JONES ST|STE 5400|Sioux City|IA|Woodbury|51101|||42496648|96400644|A|8|2|U |12/24/2009 nawk... (3 Replies)
Discussion started by: greenworld
3 Replies

5. Shell Programming and Scripting

sort date issue

Hi Everyone, # cat b Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT; Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT; Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT; # sort -t';' -k5 b Sat 13 Sep 2009 10:31:49 PM... (8 Replies)
Discussion started by: jimmy_y
8 Replies

6. UNIX for Dummies Questions & Answers

sort issue

Hi Guys , Please help me I am having a requirement as below: $ ls -i log* | head 66486532 log1 66486662 log10 66486663 log11 66486664 log12 66486665 log13 66486533 log2 66486534 log3 66486535 log4 66486584 log5 66486590 log6 This id listing the first 10 files starting with log. ... (4 Replies)
Discussion started by: mraghunandanan
4 Replies

7. Shell Programming and Scripting

performance issue using gzcat, awk and sort

hi all, I was able to do a script to gather a few files and sort them. here it is: #!/usr/bin/ksh ls *mainFile* |cut -c20-21 | sort > temp set -A line_array i=0 file_name='temp' while read file_line do line_array=${file_line} let i=${i}+1 (5 Replies)
Discussion started by: naoseionome
5 Replies

8. UNIX for Dummies Questions & Answers

unix SORT

Hey guys. I am trying to sort a file by account number through UNIX. I have a few things but it seems to sort by account number AND sort everything after the account number. Help please. Thanks (5 Replies)
Discussion started by: ndoggy020
5 Replies

9. Shell Programming and Scripting

Unix Arithmatic operation issue , datatype issue

Hi, I have a shell scripting. This will take 7 digit number in each line and add 7 digit number with next subsequent lines ( normal addition ). Eg: 0000001 0000220 0001235 0000022 0000023 ........... ......... ........ Like this i am having around 1500000 records. After adding... (23 Replies)
Discussion started by: thambi
23 Replies

10. UNIX for Advanced & Expert Users

Unix Sort - Alternatives

Hi All, I want to sort a flat file which will contain millions of records based on a key/field. For this I want to use unix sort command and before that I want to make sure that unix sort command has any file size limitations. And also please let me know whether I have to change any... (1 Reply)
Discussion started by: chprvkmr
1 Replies
Login or Register to Ask a Question