Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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,

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 ?

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

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

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
201There'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"

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

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |