Combining commands with pipes and redirection

Lead Image © Aliaksandr Herasimau, 123RF.com

Pipe Time

Special tools in the shell help you combine commands to create impromptu applications.

The Linux command line provides hundreds of small utilities to read, write, parse, and analyze data. With just a few extra keystrokes, you can combine those utilities into innumerable impromptu applications. For example, imagine you must extract an actor's lines. That is to say, given the text shown in Listing 1, you must produce That's what they call a sanity clause for Groucho. The grep command can find substrings, strings, and patterns in a text file. You can use grep to find all lines that begin with GROUCHO. Then you can use cut to divide the matching lines into pieces and combine the two commands with a pipe (|):

$ grep -i -E '^Groucho' marx.txt | cut -d ':' -f 2
That's what they call a sanity clause.

The grep clause searches the file marx.txt for all occurrences of "Groucho" that appear at the beginning of a line (-E '^Groucho'), ignoring differences in case (-i). The cut clause separates the line into fields delimited by a colon (-d ':') and selects the second field (-f 2). The pipe operator turns the output of the grep clause into the input of the cut clause.

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content