From owner-freebsd-questions Thu Mar 14 17:20:20 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mired.org (dsl-64-192-6-133.telocity.com [64.192.6.133]) by hub.freebsd.org (Postfix) with SMTP id 94CC237B405 for ; Thu, 14 Mar 2002 17:20:08 -0800 (PST) Received: (qmail 54618 invoked by uid 100); 15 Mar 2002 01:19:56 -0000 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <15505.19515.835482.32469@guru.mired.org> Date: Thu, 14 Mar 2002 19:19:55 -0600 To: Bsd Neophyte Cc: freebsd-questions@freebsd.org Subject: Re: serious problems with understanding the 'sort' command In-Reply-To: <20020315002003.94109.qmail@web20105.mail.yahoo.com> References: <20020315002003.94109.qmail@web20105.mail.yahoo.com> X-Mailer: VM 6.90 under 21.1 (patch 14) "Cuyahoga Valley" XEmacs Lucid X-face: "5Mnwy%?j>IIV\)A=):rjWL~NB2aH[}Yq8Z=u~vJ`"(,&SiLvbbz2W`;h9L,Yg`+vb1>RG% *h+%X^n0EZd>TM8_IB;a8F?(Fb"lw'IgCoyM.[Lg#r\ From: "Mike Meyer" X-Delivery-Agent: TMDA/0.48 (Python 2.2 on freebsd4) Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG In <20020315002003.94109.qmail@web20105.mail.yahoo.com>, Bsd Neophyte 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 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