Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

In Unix, how do I use the sort command?

The sort command sorts the contents of a file, in numeric or alphabetic order, and prints the results to standard output (usually the terminal screen). The original file is unaffected.

For example, if filename is a file containing a list of words, at the Unix prompt, you would enter:

sort filename

This will print the list to the screen in alphabetical order (numbers first, then capital words, then lowercase words). To eliminate any duplicate entries in the list, use:

sort -u filename

To sort case-insensitively, use:

sort -f filename

To sort case-insensitively and in reverse order, use:

sort -fr filename

As with many Unix commands, you can redirect the output to a new file:

sort filename > newfilename

The output of the sort command will then be stored in a file named newfilename in the current directory.

You can also pipe the output of the sort command into other Unix commands, for example:

sort filename | more

This sends the output through the more command for easy reading.

To print only the first word of each line, enter:

sort filename | cut -f1 -d" "

The cut command selects the field specified by the  -f  option and distinguishes fields by the delimiter character specified by the  -d  option, a space in this example.

To view the online manual for the sort command, at the Unix prompt, enter: man sort

At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?

This is document afjb in domain all.
Last modified on August 22, 2008.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.