How do I Use the Unix Sort Command?

|

Using the Unix sort command is very easy and only requires the most basic of command line knowledge. While there are a few parameters that can be passed to the sort command and a few flags that may be of use to you for more complex sorting, the basic function is very easy to use. For a pipeline sort, the command "ps -ef | sort" is used; and for a numeric sort, the command "ls -al | sort" is used.

Some of the various flags that can be used for the sort command include: -r if you wish to sort your data in the reverse order, -f if you wish to ignore the specific case of a letter, -n if you want to do a numeric sort, -b if you wish to ignore any leading blank spaces, and -M if you wish to sort by month.

Below is an example of a numeric sort:


ls -al | sort +4n | more

This Unix sort command will sort the data in ascending, or smallest to largest, numeric order. The more command is used in case the data may be too much to fit on one screen. This same function, in the reverse order, can be written as so:

ls -al | sort +4nr | more

This Unix sort command will perform the same sort over the same data as the previous sort function, but it will list the data in descending order, or largest to smallest.

0 comment:

Post a Comment

 

©2009 computer technology World | Template Blue by TNB