E-Mail:

Sorting Text Files

Sorting Text Files

The sort function in Linux can be very useful, though you might not think of using it without some prompting. That’s what Penguin Shell is all about.

I use sort hourly on my machine, via a cron job. What could possibly need sorting hourly? As you’ll recall (and as many of you have seen firsthand) I use a pretty tight mail filtering protocol on my machine. It leverages procmail and an accept list to allow only the mail into my inbox that I’ve explicitly deemed as acceptable. In other words, one of the first things procmail does is to check the accept list for the From address of an incoming email. If it’s on the list, it gets delivered to my inbox. If it’s not, a reply-to message is generated that, once received, automatically moves the From address to the accept list. This list can get quite long over a period of just a few days, making surfing through the list a chore. sort takes care of this problem by alphabetizing the accept list, making it much easier to read.

I use the following command:

cd /home/tony
sort -u .accept-list > a.tmp;
mv -f a.tmp .accept-list

This command, utilizing sort at its core, does four things:

  • [c]hanges [d]irectory to /home/tony
  • sorts, ignoring identical lines [-u] the .accept-list file
  • pipes the results of the sort to a file called a.tmp
  • [m]o[v]es the a.tmp file to a new file named .accept-list, [f]orcing the creation of new file even if the old file exists

By setting this command in a cron job, my mail .accept-list is always sorted alphabetically, ignoring case.

This, obviously, is just small and quick example of the power of sort in Linux. Play with it for a bit, and I’m sure you’ll find uses for sort that will ease simplify your own computing life.

One Comment

Microsoft Works or better Open Office can be helpful substitutes for the more popular Microsoft Office.
Command line utilities to manage text are available in Microsoft Server2003 Support Tools. Though GnuWin32.sourceforge.net Has familiar command line *nix tools for Windows.

Cheers!

What Do You Think?

 

Want to Start a Blog Here for Free?

Are you an expert in one subject or another? If your goal is to help others and dispense hard-earned information back to the community, stake a claim on your very own Lockergnome blog today! You can write about anything - no matter the topic. Sign-up to start blogging!

64 queries / 0.328 seconds.