sections in this module City College of San Francisco - CS160B
Unix/Linux Shell Scripting
Module: Linux Review
module list


You should be familiar with sort. If, however, you are used to the old-fashioned sort key syntax of +N and -N, you must switch to the newer POSIX-style key syntax using -kN. The old key syntax has been deprecated for some time and is no longer supported on the Linux version of sort.

POSIX-standard sort keys are indicated using the -kkeyspec syntax where keyspec is start[,stop][opts] where fields and characters are numbered beginning at 1.

start is field[.char] if .char is missing, the first character is indicated

stop is field[.char] if .char is missing, the end of the field is indicated. If stop is missing, the end of the key is the end of the record. 

Thus, -k2,2 indicates sort on field 2 only. 

The most common options to use are n (numeric) and r (reverse). 

Let's practice by interpreting a few sort keys:

-k2,2r  - perform an alphabetic sort on field 2 (only) in reverse (largest to smallest) order

-k3.2,3n - perform a numeric sort starting at the second character in field 3 (skip the first character). The key ends at the end of field 3 (or when there are no more numeric characters)

-k2,2n -k3r - perform a numeric sort on field 2 (only). Then break ties by an alphabetic sort beginning with field 3 and ending with the end of the record, in reverse order.

Prev This page was made entirely with free software on Linux:  
and LibreOffice    

Copyright 2016 Greg Boyd - All Rights Reserved.

Document made with Kompozer