For example, say you have a file with information about a group of people, including a name, age, zip code, and phone number for each person, that looks like this:
|
If you wanted to find all the Roosevelts and sort them by zip code, you could do it like this:
grep Roosevelt people.txt > grep.out
sort +3 grep.out
rm grep.out
Since I haven't introduced the grep and sort commands yet, here's an English translation of what's happening above:
Look for lines that contain Roosevelt in the people.txt file and put them in a file named grep.out. Then sort the grep.out file on the fourth column and display the results on the console befor e deleting the grep.out file. (Yes, it is odd that the +3 flag tells sort to use the fourth column!)
But you could avoid creating and deleting the intermediate file (grep.out) by combining the operation into one command like this:
grep Roosevelt people.txt | sort +3
The vertical bar tells the shell that the output of the program on the left (grep) should become the input for the program on the right (sort). Behind the scenes, the shell may be issuing the exact same three commands as in the previous example, but you don't really care--you've combined three commands into one.
You can have any number of steps in a pipeline, and you can even combine pipes with redirection, as shown here:
grep Roosevelt people.txt | sort +3 > sort-results
Here, the same thing happens, except that the end result is stored in a file called sort-results.
Previous Lesson: Redirection
Next Lesson: Processes
|
![]() |
|
| <Send This Link to a Friend> <Help> <Bookmark This Page> | ||