Skip to content

Wednesday, December 2nd, 2009

New Linux User

HOWTO: Use the Sort Command.

November 25, 2005 by Jon  
Filed under How To

I’m not sure if this entry should have been a ‘Geeky Fun’ posting, or a HOWTO because you can do some interesting, but possibly useless stuff with the sort command.

The sort command will display a text file sorted in a specified order. From the man sort command:

Write sorted concatenation of all FILE(s) to standard output.

I like my definition better.

Consider if I have a text file containing the names of all the cars in my garage as follows:

  • Porsche
  • Audi
  • Ferrari
  • BMW
  • Lexus

Note that the file is no particular order. If I issue the command:

sort cars

The file will be displayed in my terminal window sorted alphabetically:

Audi
BMW
Ferrari
Lexus
Porsche

Sort can also be used to sort more than one file and merge them together. For example, consider if I have a text file named ’shopping’ with the following contents:

Zebras
Carrots
Apples
Mangoes

Now if I type:

sort cars shopping

The two files will be sorted together and the output will be:

Apples
Audi
BMW
Carrots
Ferrari
Lexus
Mangoes
Porsche
Zebras

As with most GNU/Linux commands, there are a variety of switches that can be applied to the end of the sort command that affect its output. Some of the options available through these switches allow you to output to a file rather than the screen, reverse the output, consider month ordering (as in Jan is greater than Dec), only sort on alphanumeric characters, and many more.

I have used the sort command now and again, but I learned something very neat about it that I didn’t know from The Linux Documentation Project. The sort command can be used to create a text file that is sorted in real time. For example, typing:

sort > cars

Puts me into data entry mode. I can now type the name of each of my cars followed by the enter key to enter them into the cars text file. When I’m done, hitting Ctrl-D will put me back to my command prompt. Take a look at the cars text file – it will have the names of the cars I typed already sorted in alphabetical order.

Pretty neat.

  • StumbleUpon
  • Digg
  • Facebook
  • Mixx
  • Google
  • TwitThis
  • Reddit
  • Yahoo! Buzz
  • Slashdot
  • E-mail this story to a friend!

Comments

5 Responses to “HOWTO: Use the Sort Command.”
  1. paul says:

    More on sorting on certain fields here: http://gnuru.org/index.pl?node_id=844

  2. Jon says:

    Thanks, Paul!

  3. Jon says:

    I hope that’s a good comment…

Trackbacks

Check out what others are saying about this post...
  1. [...] Оригинальный текст статьи на английском языке. [...]

  2. yellowhead poker room…

    Hitlerism dueling Pascal:moderately …



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!


About Us | Advertise with us | Blog for EveryJoe | Privacy Policy | Terms of Use
Get This Theme | Sitemap


All content is Copyright © 2005-2009 b5media. All rights reserved.