• May 20, 2013

Previous

Next

Using the Sort Command

October 25, 2011, 8:00 am

Letter BlocksThere are lots of tasks that can be handled quickly by using the command line. In his recent postings here at ProfHacker on the topic, Lincoln has covered the basics. Now it is just a matter of making the command line work for you.

One handy command to know if you work with a lot of long text lists is “sort” which sorts a list of items in a text file that has each item on a separate line. If you use a Mac OS X or Linux, save the text you wish to sort in a plain text file (on the Mac you can use the included TextEdit application for working with plain text files, and if you see “Make Plain Text” in the “Format” menu then you know it will be saved in rich text format instead of plain text). It will be easier to handle your file if you don’t put any spaces in the name. The instructions below assume you use a unix based operating system like Mac OS X or Linux which I’m familiar with (DOS in Windows also has a sort command which works in a very similar way and you can get documentation for how it works by typing “help sort” in your terminal).

Open up your terminal application to access the command line, and use the “cd” command to navigate your way to the folder which contains the text file you wish to sort. If you then type:

sort [name of file to be sorted]

It will output the sorted results directly in the terminal. If you want, instead, to save that output in a new file, you can use what is known as redirection with the “>” sign; redirecting the output into a new file.

sort [name of your file] > [name of the new file]

Again, it is easiest if you avoid spaces. Once you run the above command your sorted text will be in a new file in the same directory as the old one.

As with all things on the command line, however, there are endless ways to customize your sort. As with many commands, you can read the documentation for the sort command with “man sort” which will show you some of the options.

Want to ignore case? Try sort -f [your file]
Want to reverse the sort? Try sort -r [your file]
Have a bunch of numbers or numbers mixed with text, some with leading zeroes? Try sort -n [your file]

There are others ways to do this without resorting to the command line, of course. If you have multiple columns of information and want to sort by some chosen column, a spreadsheet application is your best bet. Even if you just have a simple text list that is a single column, if you have an internet connection you can copy and paste in some text in an online tool like TextMechanic. If you use Microsoft Word and have your list in a document, you select the list and use the sort button. If you use Libre/Open/NeoOffice you can sort tables in text documents via the “Tools” menu, or again do your sorting in a spreadsheet.

Next time around, I’ll talk a little bit about some other simple commands to manipulate text that I have found useful. Do you have any simple favorites that you use?

Image: Letter Tower, a Creative Commons image from mikefischer’s photostream

This entry was posted in Software and tagged , , , , , . Bookmark the permalink.

  • 1255 Twenty-Third St, N.W.
  • Washington, D.C. 20037
subscribe today

Get the insight you need for success in academe.