Linux Basics (tutorial) #11

Discuss everything about Linux here!

Moderator: Community Moderator

Post Reply
User avatar
MrNiitriiX
Premium Uploader
Premium Uploader
Posts: 2197
Joined: 19 Apr 2010, 14:52
Location: between space and time
Has thanked: 24 times
Been thanked: 164 times

Linux Basics (tutorial) #11

Post by MrNiitriiX »

Will Linux Pipelines Make Me Filthy Rich?
Pumping a Program's Output into Another Program
Linux provides you with a wide array of utilities to manipulate data. You can search, sort, slice, dice, and transform data stored in files in many different ways. A pipe (also called a pipeline) is a powerful shell feature that allows you to p ump the output of one program directly into another.

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:

Code: Select all

Roosevelt     Tommy 	38 	54579 	555-1212
Nixon           Edward 	19 	37583 	246-3457
Roosevelt     Freddie 	47 	11745 	674-6972
Lincoln         Albert 	26 	26452 	916-5763
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.

LINKS TO THE OTHER TUTORIALS


Leason 1: Living in a shell
Leason 2: Root and Other Users
Leason 3: Virtual Consoles
Leason 4: Logoff and Shutdown
Leason 5: Choosing a Shell
Leason 6: The Command Prompt
Leason 7: Wildcards
Leason 8: Command History and Editing
Leason 9: Aliases
Leason 10: Redirection
Leason 11: Pipelines
Leason 12: Processes
Leason 13: Stopping a Program
Leason 14: Environment Variables
Leason 15: Help!
Last edited by MrNiitriiX on 31 Jul 2010, 13:03, edited 1 time in total.
"Injustice anywhere is a threat to justice everywhere." - Martin Luther King
Image
Upload List
Post Reply

Return to “Everything Linux”