LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash "sort" command problem (https://www.linuxquestions.org/questions/linux-newbie-8/bash-sort-command-problem-4175457910/)

rt1870 04-12-2013 08:13 AM

Bash "sort" command problem
 
I am trying to use a piece of scientific software that involves executing a number of scripts and commands.There is a particular step I am stuck on, where the program needs a file to be sorted and the documentation gives the command:

sort +3 -25 file1 >file2

The options +3 and -25 do not seem to correspond to any of the options for "sort" when I look at the help in bash. Am I missing something simple here? Is this not actually a bash command and something else? Unfortunately the documentation does not explain the way in which the data needs to be sorted, so I am unable to implement my own solution.

I am fairly new to using bash and linux in general and am completely stuck. Any help appreciated!

bigrigdriver 04-12-2013 09:56 AM

Quote:

I am trying to use a piece of scientific software that involves executing a number of scripts and commands.There is a particular step I am stuck on, where the program needs a file to be sorted and the documentation gives the command:

sort +3 -25 file1 >file2
It would help if you identified that "scientific software" so that others can examine the documentation.

rknichols 04-12-2013 10:09 AM

sort is a separate program and not something internal to bash. The command line you were given uses an obsolete syntax, and specifies a sort beginning with field 3 and extending up to but not including field 25, where the fields are numbered from 0. The current manual page for sort no longer describes this syntax, though the sort command itself does still accept it. You can find a page that includes the obsolete syntax here.

The modern equivalent would be
Code:

sort -k4,25 file1 >file2
The field numbers here start from 1, and the sort is inclusive of the starting and ending field numbers.

konsolebox 04-12-2013 10:12 AM

I think they are key field numbers and are somehow similar to the values presented with -k only that the position is n + 1. e.g. sort +3 -25 is similar to sort -k4,25. The numbers presented in -k represent the key fields of the line which are prioritized in sorting before the whole line is sorted in general.

So if I have a file containing
Code:

aaa        def        ghi
bbb        abc        ghi
aaa        abc        jkl

Code:

sort +1 -3 file.txt
sort -k 2,3 file.txt
sort -k 2 file.txt

will have
Code:

bbb    abc    ghi
aaa    abc    jkl
aaa    def    ghi

and
Code:

sort +1 -2 file.txt
sort -k 2,2 file.txt

will have
Code:

aaa    abc    jkl
bbb    abc    ghi
aaa    def    ghi


David the H. 04-14-2013 08:31 AM

Try reading "info sort". It describes the usage of the program in a lot more detail than the manpage.

This is true of most info pages, by the way.

rt1870 04-17-2013 07:12 AM

Just a bump to say thank you to the replies - answered my question perfectly (especially konsolebox!).


All times are GMT -5. The time now is 08:41 AM.