LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2013, 04:28 AM   #1
ramkisak
LQ Newbie
 
Registered: Mar 2013
Posts: 8

Rep: Reputation: Disabled
Question Sorting a file based on column number


I am able to sort a file by defining a starting column number in MSDOS. However I only know to sort a file through a field number in Linux. Is there any option in Linux to achieve what i have achieved through DOS. For example, I can say

sort /+21 filename
and this will sort using the info from column21 onwards of each line in the file. The reason for this is the number of fields in all the lines in the file to be sorted are not the same (output of a Fortran Program)
 
Old 07-30-2013, 04:41 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
man sort
Code:
       -k, --key=POS1[,POS2]
              start a key at POS1 (origin 1), end it at POS2 (default end of line).  See POS syntax below
so I guess...

Code:
sort -k 21 filename
 
Old 07-30-2013, 10:36 PM   #3
ramkisak
LQ Newbie
 
Registered: Mar 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Question

I have been using the -k option, but this is applicable when the lines are separated by fields delimited by field separator such as space or tabs. In my case the lines in my file have different number of fields, when the space is considered as a field separator. However the information that I want the sorting to be based upon are in the same column in each line, hence DOS sort does the job. However I want to know how to achieve this in LINUX if there is a proper option in sort. Since I could not find one, hence this post
 
Old 07-31-2013, 01:14 AM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,574

Rep: Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489
Continuing with man sort
Quote:
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and
the stop position defaults to the line's end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding
whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which override global ordering options for that key. If no key is given,
use the entire line as the key.
Set the field separator so that the whole line is one field e.g. using the ASCII NUL
Code:
sort -t '\0' -k 1.21 <filename>

Last edited by allend; 07-31-2013 at 01:16 AM.
 
Old 07-31-2013, 01:16 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by ramkisak View Post
I have been using the -k option, but this is applicable when the lines are separated by fields delimited by field separator such as space or tabs. In my case the lines in my file have different number of fields, when the space is considered as a field separator. However the information that I want the sorting to be based upon are in the same column in each line, hence DOS sort does the job. However I want to know how to achieve this in LINUX if there is a proper option in sort. Since I could not find one, hence this post
You don't show any relevant example and do not mention why the given solution does not work.

The -k<number> (-k21) option tells sort to start sorting at column 21 (and everything after column 21 if needed!!).
The -k<number>,<number> (-k21,21) option sorts only column 21.

Without knowing what the input is like and how, exactly, you want this to be sorted we can only give you a global answer (which was given by Firerat).
 
Old 07-31-2013, 02:16 AM   #6
ramkisak
LQ Newbie
 
Registered: Mar 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Bingo!! The command sort -k1.n filename, where n is the column number worked fine though the number of blanks in the various lines are not the same. Thanks for the responses that I appreciate very much
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Faster bash sort based on column number akeka Programming 13 05-15-2012 02:48 PM
[SOLVED] Read a number x from a file and write a character in the xth column of other file homexylo Programming 7 08-05-2011 05:28 AM
[SOLVED] sorting a first non-numerical column in a file sasanthi Linux - Newbie 8 07-13-2011 06:39 AM
sorting a file by column AutoC Programming 6 08-07-2009 05:43 PM
Find Number of column in a file Swapna173 Linux - Newbie 3 06-04-2008 01:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration