link to content
Essentials at NC State Home
skip header navigation and go to content Help | ResNet | Computing@NC State | For OIT Staff | Publications | Search NC State | Feedback | Site Map
your unity account
antivirus & security
email & messaging
connections & labs
your computer
software@nc state
files
web pages
education & training
publications
other resources
troubleshooting
ITD Sections

Sorting

The sort command can sort lines of text and can check whether a file is sorted.

To specify the type of sorting you want performed, add any of the following options:

 
b	ignore leading blank spaces when sorting
 
d       ignore all characters except letters, digits and 
        blanks when sorting

n	sort according to arithmetic value of each line's 
        initial numeric string

r	sort in reverse (descending) order 

For example, to sort the words in the "wordlist" file in reverse order, and display the sorted lines to the screen, you would enter

sort -r wordlist

[sparc03]...kaputnik>sort -r wordlist
postulate
frankfurter
exhale
dumpling
alabaster
[sparc03]...kaputnik>

To sort a file and display the sorted lines on the screen, enter

sort filename

after replacing filename with the name of the file to sort. The results will be displayed on your screen.

[sparc03]...kaputnik>sort wordlist
alabaster
dumpling
exhale
frankfurter
postulate
[sparc03]...kaputnik>

To see if a file is in sorted order, follow the sort command with the -c option. If the file is not sorted in ascending order, the sort command replies that there is a disorder.

[sparc03]...kaputnik>sort -c wordlist
sort: disorder on wordlist
[sparc03]...kaputnik>

To save the sorted information to a file, include the -o output_file option, replacing output_file with the name of the file for storing the output.

For example, entering

sort -o sortedwords wordlist

would create a file named "sortedwords" that contains a sorted version of the file "wordlist."

 

Last modified July 20, 2004 by cawalker

jump back to content/page ends, begin footer
jump to content
jump to content Go to page top Page Top | Site Map | OIT | Policy Disclaimer | Site Survey