Database Reference
In-Depth Information
from its ability to combine these small yet powerful command-line tools. The most
common way of combining command-line tools is through a so-called pipe . The out‐
put from the first tool is passed to the second tool. There are virtually no limits to
this.
Consider, for example, the command-line tool seq , which generates a sequence of
numbers. Let's generate a sequence of five numbers:
$ seq 5
1
2
3
4
5
The output of a command-line tool is by default passed on to the terminal, which dis‐
plays it on our screen. We can pipe the ouput of seq to a second tool, called grep ,
which can be used to filter lines. Imagine that we only want to see numbers that con‐
tain a “3.” We can combine seq and grep as follows:
$ seq 30 | grep 3
3
13
23
30
And if we wanted to know how many numbers between 1 and 100 contain a “3”, we
can use wc , which is very good at counting things:
$ seq 100 | grep 3 | wc -l
19
The -l option specifies that wc should only output the number of lines. By default it
also returns the number of characters and words.
You may start to see that combining command-line tools is a very powerful concept.
In the rest of the topic, you'll be introduced to many more tools and the functionality
they offer when combining them.
Redirecting Input and Output
We mentioned that, by default, the output of the last command-line tool in the pipe‐
line is outputted to the terminal. You can also save this output to a file. This is called
output redirection, and works as follows:
$ cd ~/book/ch02
$ seq 10 > data/ten-numbers
Here, we save the output of the seq tool to a file named ten-numbers in the directory
~/book/ch02/data . If this file does not exist yet, it is created. If this file already did
Search WWH ::




Custom Search