Archive for January, 2007

Pipes

If you're new here, you may want to subscribe to my RSS feed.

I mentioned previously that Unix uses lots of text files, which are easy to create and manipulate. Unix has a variety of commands to manipulate text files. The way to move text between these commands is with pipes. The pipe symbol is the vertical bar (|) above your Enter key.

For example, suppose you have a text file full of email addresses, with one email address per line. Suppose it’s named “emails.txt” and it’s on your Desktop.

To view the emails, type the following command in the Terminal:
cat ~/Desktop/emails.txt

To view the emails sorted alphabetically, type the following:
cat ~/Desktop/emails.txt | sort

The pipe causes the output of the cat command to be fed into the sort command, then outputted to the screen. You can tack on as many pipe commands as you need.

To view the emails sorted alphabetically and with a prompt between each page, type:
cat ~/Desktop/emails.txt | sort | more

The list of emails “flows” from the cat command to the sort command to the more command, as if going through water pipes.