next up previous contents
Next: Job Control: Background and Up: Unix Filters Previous: grep

sort

A filter for sorting alpha-numeric text fields is appropriately called sort. sort accepts input from stdin by default, so it can be used in a chain of commands, or it can accept input from a file:

cat <file1> <file2> | sort | more

or the equivalent:

sort <file1> <file2> | more

By default, the sort is done according to the character or numeric value in the leftmost column of a field. Fields are separated by tab or space characters by default, but any other field separators can be used by defining them on the command line.

Useful command-line options for sort are as follows:


   		 -b 		 Ignore leading space characters in the starting
		 		 and ending positions of a field.
		 -d 		 Dictionary order.  Only letters, digits, space, and tab
		 		 are significant in the sort.
		 -f 		 Treat upper and lower case characters as equivalent.
		 -n 		 Numeric sort. Sort by arithmetic value.
		 -r 		 Reverse the order of the sort.
		 -t<c> 		 Use the character <c> as the field delimiter.
		 +sp.o 		 sp  is the starting position for the sort. +0 is
		 		 leftmost field. .o is the optional character offset
		 		 into a field which indicates where the sort should begin.
		 -ep.o 		 ep  specifies the field number before which the
		 		 sort is ended. .o is optional; it specifies that the
		 		 sort will end at the character just prior to the .o
		 		offset into the ep  field. 
EXAMPLES:

sort -d +0 -1 file1 | more

The sort will start at the first field, and end before the second.

sort -d +0.1 -0.2 file1 | more

The sort starts at the first character of the first field, and ends after that same character.


next up previous contents
Next: Job Control: Background and Up: Unix Filters Previous: grep
World Wide Web Administrator
1/5/1999