Will Linux Pipelines Make Me Filthy Rich?
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
Comments - most recent first
(Please feel free to answer questions posted by others!)
open failed: +3 No such file or directory
Thanks for the Tutorial, is great
Thank you very much Dr. Bob!
I've got the same error:
open failed: +3: No such file or directory
Who can I do that?
open failed: +3: No such file or directory
When you add a -4 (as the second position flag) the command works. Do you Bob still use this syntax or do you now use the -k flag?
I just enter the linux world, and the information is very accessible.
does it overrides ?
grep Roosevelt < people.txt | sort +3
< - is missing
I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But
don't ask me to send what you already have. :-) NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.

