Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Mar 2002 19:19:55 -0600
From:      "Mike Meyer" <mwm-dated-1016587196.02aaf5@mired.org>
To:        Bsd Neophyte <bsdneophyte@yahoo.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: serious problems with understanding the 'sort' command
Message-ID:  <15505.19515.835482.32469@guru.mired.org>
In-Reply-To: <20020315002003.94109.qmail@web20105.mail.yahoo.com>
References:  <20020315002003.94109.qmail@web20105.mail.yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help
In <20020315002003.94109.qmail@web20105.mail.yahoo.com>, Bsd Neophyte <bsdneophyte@yahoo.com> typed:
> the following two options are killing me:
> 
> -n        performs a numeric sort.
> (+|-)n    begins (+n) or ends (-n) the sort with the field following the n
> 
>           field
> 
> first i would like some clarification with the (+|-)n option.
> in the first example, the +4 says that we start counting from the 5th
> field (5th field because we start counting from 0, not 1 right?)... so
> when we count, we use fields 5,6,7 right?
> sort -rn +4 list -o num.list

No, -4 is not where you start counting, but the field you use for
sortingbut that you start *sorting*. I.e. - you're going to sort the
text numerically starting with the fifth column to the end of the
line. Let's work on /etc/passwd, stripping comments. We want to use :
as a separator, not whitespace, so we add "-t :" to the mix, and get:

sed '/^#/d' | sort -t : -rn +3

That will output the password file sans comments, sorted by the group
field, as it's the fourth field.

> in this example, the -3 says we start counting from the 4th field
> backwards right?  so 3,2,1,0?
> sort -rn -3 list -o num2.list

When I try that one, sort gives me an error message, and righly
so. You can't do either +n or -n, you can only do -n if you've already
done +n. The -n specifies where the sort key is going to end. And it's
absolute. So if you do this:

sed '/^#/d' | sort -t : -rn +3 -5

restricts the rest of the sorting to the fourth and fifth
columns.

Generally, having sort pull the keys out of the lines sucks. It's
faster to choose some character you know isn't going to be in the
keys, then use awk or cut and paste to pull the keys and prepend them
to the lines, sort the lines, and use sed to remove the sort keys.


> next, i'm confused with what I see with the first two examples and this
> one.  i notice that in the first two examples the +/- numbers (+4 and -3)
> are isolated options, but in the last example, +5n, the two options i
> mentioned earlier are combined.  i don't understand why this is done, or
> what it means.  shouldn't it be +5 -n instead of +5n?  is +5 -n different
> from +5n?  

They should all mean the same thing. Then again, if your text gave the
second example above, then what it's documenting and what you're using
aren't the same thing

	<mike
--
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15505.19515.835482.32469>