[Linux-users] Sort Question

Sean Keating sean
Fri Aug 3 07:59:56 PDT 2007


The sort command sorts lines of text.  So, in your example, it is doing
exactly what you told it to do.  You need to break each line into its
parts and sort that.  Try something like:


#! /bin/bash

#first clear the the output files
> js2
> sortfile.txt

for w in `cat sourcefile.txt`
do
  # clear the temp file
  > js1

  #first break each line into individual numbers
  for n in `echo $w|sed 's/:/ /g'`
  do
    echo $n >> js1
  done

  #sort the numbers in the line and write them to temporary output file
  for o in `sort -n js1`
  do
    echo -en "${o}:" >> js2
  done
  # add a linefeed to the end of the line
  echo -en "\n" >> js2
done

# remove the trailing :
cat js2|sed 's/:$//' > js3

# now sort the lines
sort js3 > sortfile.txt

# do cleanup
rm js1 js2 js3

#display the output
cat sortfile.txt




On Thu, 2007-08-02 at 14:01 -0700, tom marinis wrote:
> I think is fairly simple problem but I simply can't get my head 
> around it.  I guess my brain needs to go in for some warranty 
> work :)
> 
> Maybe someone here can point me to a newgroup for further tips
> or make a helpful suggestion?
> 
> Here it is.
> 
> I have a listing of number in a textfile, lets say something
> like this;
> 
> less sourcefile.txt
> 
> 3:51:12:74:41:43:13
> 8:33:36:37:39:41:9
> 1:6:23:24:27:39:34
> 
> etc...
> 
> 
> My thinking is to use "sort", using the numeric option.
> 
> Reading the man instructions, they say that I can use 
> options -gn, but -g or -n  simply does the same thing.
> When I try to "sort" the textfile, I get something 
> like this;
> 
> $ sort -t: -g inputfile.txt > outputfile.txt
> 
> 1:6:23:24:27:39:34
> 3:51:12:74:41:43:13
> 8:33:36:37:39:41:9
> 
> Which is nice, but what the results I expected to 
> generate are something different.  I need to sort by 
> field as well, so what I want to see from output 
> is something like this;
> 
> 1:6:23:24:27:34:39
> 3:12:13:41:43:51:74
> 8:9:33:36:37:39:41
> 
> 
> Do I use something else other than sort, or have I missed
> something about sort that's very obvious?
> 
> I'm trying to review the archives, but I don't have a 
> clue where to find this topic mentioned anywhere.
> 
> Thanks muchly...
> 
> Tom.
> 
> 
>        
> ____________________________________________________________________________________
> Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.
> http://smallbusiness.yahoo.com/webhosting 
> _______________________________________________
> Linux-users mailing list
> Linux-users at linux-sxs.org
> http://mail.linux-sxs.org/cgi-bin/mailman/listinfo/linux-users
-- 
Sean Keating
Computer Support, Inc
Office: 401-885-2297
Cell:   401-225-3545
<sean at csupport.com>





More information about the Linux-users mailing list