You are not logged in or registered. Please login or register to use the full functionality of this Website SybaseTeam.Com...
Hello There, Guest! (LoginRegister) Remember Me? Current time: 09-21-2014, 09:07 AM
   Home  |  About Us  |  Sybase Training  |  Synergy  |  Consulting  |  Job Openings  |  Tech Videos  |  Rules and Disclaimer  |  Search
Post Reply 
Forum Tools
Powerful usage of sort command in Unix - column wise sorting
04-24-2010, 01:08 PM
Post: #1
Quote this message in a reply
Powerful usage of sort command in Unix - column wise sorting


We know that sort can sort the lines of a file alphabetically.
But here is another more powerful usage of sort. It can sort a file as columns, just like we sort columns in excel.

Let's say below is your file content (test)

A,1
JHH,100
B,3
C,4
B,10
A,20



You want to sort it based on the first column alphabetically and then by second column numerically. Columns are divided by , (comma)
Below is the sort command to do that

sort -t, -k1,1 -k2,2g test

-t specifies the delimiter for columns
-k1,1 specifies that column 1 should be sorted (default sorting alphanetically)
-k2,2g specifies that column 2 should be sorted numerically. (g for general-numeric-sort)

The output would be as below.

A,1
A,20
B,3
B,10
C,4
JHH,100


Hope this helps.


Find all posts by this user
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  'wc' command srama2 0 5,681 06-17-2010 03:06 AM
Last Post: srama2
  'which' command srama2 0 1,741 05-27-2010 08:32 AM
Last Post: srama2
  scp command to transfer files srama2 0 3,106 05-17-2010 08:00 AM
Last Post: srama2
  last command srama2 0 2,003 05-07-2010 06:24 AM
Last Post: srama2
  listing files using 'ls' command srama2 0 2,724 04-23-2010 03:13 AM
Last Post: srama2
  viewing large files in unix srama2 0 3,849 04-21-2010 03:31 AM
Last Post: srama2
  'rmdir' command srama2 0 2,011 04-20-2010 03:18 AM
Last Post: srama2
  Processing Shell Script Options Using getopts Command srama2 0 2,884 04-19-2010 03:41 AM
Last Post: srama2
  Editing files in unix srama2 0 1,576 04-16-2010 04:00 AM
Last Post: srama2
  cal command srama2 0 2,067 04-14-2010 03:34 AM
Last Post: srama2

Options:
Forum Jump:


Contact Us | SybaseTeam | Disclaimer & Rules | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication