Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

doubt in sort command!!!

5 posters

Go down

doubt in sort command!!! Empty doubt in sort command!!!

Post by Abhiramy 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

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

Post by akalya 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

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

Post by akalya 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

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

Post by Saradha Kannan 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

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

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

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

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

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

https://unixcpp.forumotion.com

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

Post by akalya 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

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

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

thanks to all!!!its workin!! Smile

Abhiramy

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

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

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

@akalya : Thanks.. got it Smile

Saradha Kannan

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

Back to top Go down

doubt in sort command!!! Empty prbs ! minor key not working!

Post by mightyganesh 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?
mightyganesh
mightyganesh

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

http://ganeshguna.blogspot.com

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

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

try this command,

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

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

https://unixcpp.forumotion.com

Back to top Go down

doubt in sort command!!! Empty Re: doubt in sort command!!!

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

tnx! christ!
mightyganesh
mightyganesh

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

http://ganeshguna.blogspot.com

Back to top Go down

doubt in sort command!!! Empty 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