Pipeline "|" versus output redirection ">"

Question

The uses of output redirection, >, and pipelining, |, seem similar. What is the distinction in their use?

Answer

The distinction here is between storage and use as input. Consider the following example

cat volcanoes.txt | wc

from this lesson. Here we’re using the result of the left side, cat volcanoes.txt, as input to the right side, wc. If we instead wrote

cat volcanoes.txt > wc

then we would use the result of the left side and store it into a file called wc. This is a very different behavior than what we did with | above.

28 Likes

I was confused by this lesson:
$ cat volcanoes.txt | wc | cat > islands.txt

if we want to “pipe” the standard output of volcanoes.txt to islands.txt, could we write
$ cat volcanoes.txt | wc > islands.txt ? cat islands.txt turns out the same result as the lesson.

And please explain to me what the difference between storage and use as input?

4 Likes

I’m confused about this as well. Could someone help plz?

3 Likes

wc fav-cities.txt > cities.txt appears to do the same thing. Is the piping just to create a use case for it? Is there a better example of piping that does not contain a command such as wc, since wc gives output already?

3 Likes

I am joining the earlier question. Also, confused about the role of wc in this setting. It was written in the initial explanation " the wc command outputs the number of lines, words, and characters in volcanoes.txt , respectively", but still I am not really getting it. @zjedwin I would appreciate more information on this redirection command.

3 Likes

So… Is it safe to say that …

$ cat boats.txt | sort > sorted-boats.txt

Just means …

Display contents of “boats.txt”

WHILE - organizing the content inside “boats.txt”

THEN - Create a new .txt called “sorted-boats.txt” with the contents that we organized in “boats.txt” inside

???

Drawing on (reading) the content from boats.txt, sorting it and storing it in sorted-boats.txt. The original file will remain untouched.

4 Likes

The lines
cat volcanoes.txt | wc | cat > islands.txt
and
cat volcanoes.txt | wc > islands.txt
do the same thing. There will often be many ways to accomplish any task. No need to be confused.

For the difference between “storage” and “use as input”, simply note that there is a difference between saving something for later and using it immediately and then throwing it away.
With x | y, you’re saying: take the output of x and use it as the input for y. You don’t save the output of x; you’re just passing it along to y. So you’re using it as input rather than storing it someplace.
On the other hand, x > y says: save, i.e. store, the output of x into the location defined by y.

9 Likes

wc is not a redirection command. It simply provides some statistics on an arbitrary file that is passed to it. In particular
number_of_lines number_of_words number_of_characters file_name
Simply test what the output of this function shows for an arbitrary file on your machine. Like this:
wc my_file
Redirection comes from operators like | and > which simply allow you to chain commands together.

1 Like

I think I understand the use of the pipeline command well but what I don’t understand is why some of the exercises add unneccesary steps like doing
$ cat text_file.txt | sort ETC.
instead of just starting with
$ sort text_file.txt ETC.
I was able to complete these challenges without the extra useless steps and wasn’t sure what the point of those steps were; was it just to make it easier for beginners to understand by showing the whole flow of steps?, or to get us used to a certain conventional way of using the command line?, or just an accident?

4 Likes

can I also write this then?

$ sort boats.txt > sorted-boats.txt