Computing.Net > Forums > Unix > Sorting issues

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sorting issues

Reply to Message Icon

Name: poornimajayan
Date: November 9, 2009 at 15:40:27 Pacific
OS: KSH
Product: Creative Vado pocket video cam pink camcorder
Subcategory: Software Problems
Comment:

Hi,
I have a file which has data as
path/aa1.txt
path/aa2.txt,...(each in separate lines)
I want a unix script to sort this file in order. It is not working when I give
"sort filename"

Thanks,



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 9, 2009 at 20:21:04 Pacific
Reply:


If you are sorting the contents of filename, redrect the contents to sort this way:

sort < filename


0

Response Number 2
Name: tvc
Date: November 10, 2009 at 03:36:48 Pacific
Reply:

Question is not clear ... HOW is it incorrect ? The SORT command would be correct for most cases, but I'm guessing the theadstarter needs something special.

What ?


0

Response Number 3
Name: poornimajayan
Date: November 10, 2009 at 08:19:16 Pacific
Reply:

My file has values like below and I want 101 after 100.Tried "<" but no luck. (:

/home/dd/1/1.xml
/home/dd/1/10.xml
/home/dd/1/100.xml
/home/dd/1/1000.xml
/home/dd/1/101.xml


0

Response Number 4
Name: nails
Date: November 10, 2009 at 14:56:17 Pacific
Reply:

The problem you are having is that you are mixing character and numeric data. Let me think about it.




0

Response Number 5
Name: tvc
Date: November 11, 2009 at 03:48:38 Pacific
Reply:

Ow, indeed, the old numeric-charcaters-sorting-problem ..

One way of solving this issue, is to not write "1" but "00001", and not "11" but "00011".
The key here is to always have the same number of digits. What that number should be, is for you to determine, but be sure all numbers will fit, or you will have the "nobody will ever use more than 640k of memory" issue. But, I'm going away from the core of the issue here.

Alternatively, you should strip the numbers from the text, and then sort again ... because, the SORT command can do a "smart" sort, based on numbers only. Example, he CAN sort to come to this conclusion:

1
2
11
100
111
120
200
201

There's some parameter in SORT to do that. But, you must only have numbers!

For the stripping, there's lot of ways, this will work:

sed "s/\/home\/dd\/1\///g"
sed "s/\.xml//g"


0

Related Posts

See More



Response Number 6
Name: poornimajayan
Date: November 11, 2009 at 11:25:57 Pacific
Reply:

Thanks Nails & Tvc.I did like you mentioned."0000" logic.Cool.


0

Response Number 7
Name: nails
Date: November 11, 2009 at 13:33:11 Pacific
Reply:

I did a variation of what tvc suggested. If you want to keep the original text, you need to do more than strip out the numbers. Here is what the following kludge does:

1) save a copy of the last field, $NF,.
2) strip out teh .xml extension leaving a number.
3) print the original line with a new 2nd field which is numeric.
4) sort numerically by the second field.
5) discard the second field with the last awk script.

Yes, it is a kludge, but maybe somebody smarter than I can do something better:

#!/bin/ksh

nawk ' BEGIN { FS="/" }
{
   var=$NF
   gsub(".xml","", var)
   printf("%s  %d\n", $0, var)
} ' datafile.txt | sort -n -k 2,2 | nawk ' { print $1 } '

I am using nawk because of solaris


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Sorting issues

Renaming files with awk www.computing.net/answers/unix/renaming-files-with-awk/8036.html

Sort Order Presevation www.computing.net/answers/unix/sort-order-presevation/7123.html

Creating a script to sort a file www.computing.net/answers/unix/creating-a-script-to-sort-a-file/4361.html