Computing.Net > Forums > Unix > Sort keys provided from a file

Sort keys provided from a file

Reply to Message Icon

Original Message
Name: srkumar422
Date: January 31, 2008 at 04:42:46 Pacific
Subject: Sort keys provided from a file
OS: unix
CPU/Ram: dontknow
Model/Manufacturer: dontknow
Comment:

Hi, my requirement is i need to give differnet keys depending upon filename. For ex, if filename=fil1, key feilds =1,3,4,5. for file2 key feilds are 1,2,3,4,5, for file3 they are 1,3,7. I can hardcord these values in differnet sort commands like
sort -k1,1 -k3,5 -for file1 and
sort -k1,5 -for file2 and
sort -k1,1 -k3,3 -k7,7.
I want to achieve this dynamically. Means i will use only one sort command like
sort -k$start_key,$end_key
now i can pass values to start_key and end_key by putting these values and filenames in another file.

Here my question is : is there any way to get the values for key values from a file.
sort -k (here i need to give filename where key values are defined).

Thanks


Ravi Siluveru
Mumbai India


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: January 31, 2008 at 06:55:33 Pacific
Subject: Sort keys provided from a file
Reply: (edit)

You can build a variable that sort can use; here is an example of sorting myfile with the first field:

#!/bin/ksh

str="1,1 my.file"
sort -k $(echo "$str")

You can build variable str from a file's contents.

In your case, if the above fails, try eval'ing str:

#!/bin/ksh

str="1,1 my.file"

sort -k $(eval echo "$str")


Report Offensive Follow Up For Removal

Response Number 2
Name: srkumar422
Date: February 3, 2008 at 21:41:51 Pacific
Subject: Sort keys provided from a file
Reply: (edit)

Hi nails
the first way worked for me. Thanks

Ravi Siluveru
Mumbai India


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Sort keys provided from a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software