doubt in sort command!!!

Go down

doubt in sort command!!!

Post by Abhiramy on Fri Mar 05, 2010 7:00 pm

wat is the option used in sort cmnd to sort based on a particular key???

xxx 23 63
yyy 24 72
zzz 25 70
aaa 27 75

$sort -n student

xxx 23 63
zzz 25 70
yyy 24 72
aaa 27 75

here the sorting is done based on field 3 contents defaultly,,,can we change this???if so,,,how???

Abhiramy

Posts : 9
Points : 21
Join date : 2010-02-26

View user profile

Back to top Go down

Re: doubt in sort command!!!

Post by akalya on Fri Mar 05, 2010 7:02 pm

sort -k1 student
this wud sort records based on the 1st field values

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: doubt in sort command!!!

Post by akalya on Fri Mar 05, 2010 7:03 pm

sort -k1n -k2 student
this sorts student using 2nd column values as major key and 1st as minor key

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: doubt in sort command!!!

Post by Saradha Kannan on Fri Mar 05, 2010 7:12 pm

sort -k1n -k2 student
What does dis exactly do? Can u explain dis wit an example pls?

Saradha Kannan

Posts : 25
Points : 25
Join date : 2010-03-04

View user profile

Back to top Go down

Re: doubt in sort command!!!

Post by Christopher on Fri Mar 05, 2010 7:16 pm

check ur file content.. by default it ll sort first column..
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: doubt in sort command!!!

Post by Christopher on Fri Mar 05, 2010 7:22 pm

if u want to use '|' as delimiter, try this command.

Code:
$ sort -t '|' -k2 student
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: doubt in sort command!!!

Post by akalya on Fri Mar 05, 2010 7:32 pm

cat student
akalya 21 f
shiva 22 m
deva 23 m
maithreyi 22 f

sort -k1 student
akalya 21 f
deva 23 m
maithreyi 22 f
shiva 22 m

sort -k3 -k1n student
akalya 21 f
maithreyi 22 f
shiva 22 m
deva 23 m

@sarada it initially sorts using field3 as key then uses this output to sort fields based on field 2 values

hope u get it!! Question

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: doubt in sort command!!!

Post by Abhiramy on Mon Mar 08, 2010 11:40 am

thanks to all!!!its workin!! Smile

Abhiramy

Posts : 9
Points : 21
Join date : 2010-02-26

View user profile

Back to top Go down

Re: doubt in sort command!!!

Post by Saradha Kannan on Mon Mar 08, 2010 6:38 pm

@akalya : Thanks.. got it Smile

Saradha Kannan

Posts : 25
Points : 25
Join date : 2010-03-04

View user profile

Back to top Go down

prbs ! minor key not working!

Post by mightyganesh on Tue Mar 23, 2010 4:18 pm

hey

[336474@oracleclient ~]$ cat student
ganesh|21|07|m|
dinesh|22|05|m|
usha|23|42|f|
malthi|22|35|f|
preeti|22|12|f|
paru|22|40|f|
madhu|22|33|m|

[336474@oracleclient ~]$ sort -t '|' -k4n -k2 student
ganesh|21|07|m|
dinesh|22|05|m|
preeti|22|12|f|
madhu|22|33|m|
malthi|22|35|f|
paru|22|40|f|
usha|23|42|f|

i wanted to sort using second col as major key and 4th col as minor key!

but its sorting 2nd as major and 3rd col as minor key how come its possible?? or is the syntax wrong?
avatar
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 30

View user profile http://ganeshguna.blogspot.com

Back to top Go down

Re: doubt in sort command!!!

Post by Christopher on Tue Mar 23, 2010 4:49 pm

try this command,

Code:
$ sort -t '|' -k2n -k4 student
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: doubt in sort command!!!

Post by mightyganesh on Tue Mar 23, 2010 4:58 pm

tnx! christ!
avatar
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 30

View user profile http://ganeshguna.blogspot.com

Back to top Go down

Re: doubt in sort command!!!

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum