A Ali
A Ali

Reputation: 297

Understanding Pipes in linux

This may be a very simple question, but I don't understand what exactly is going on here, although I understand the commands yes, nl, and head individually.

yes | nl | head -1000 > data1.txt     

I don't understand how the pipe is interacting through all of these to make a data file with numbers 1-1000 on different lines with y next to each:

 1  y
 2  y
 3  y
 4  y
 5  y
 6  y
 7  y
 8  y
 9  y
10  y
11  y
12  y
13  y
14  y
15  y
16  y
17  y
18  y
19  y
20  y
21  y
22  y
23  y
24  y

etc.. up to 1000

Any explanation is appreciated.

Upvotes: 0

Views: 498

Answers (2)

rakib_
rakib_

Reputation: 142725

| is used for piping i.e used for communicating between multiple processes, on simple word, you can pass one process's output to another process's input.

Now "yes" man page says:

"Repeatedly output a line with all specified STRING(s), or `y'."

Since you haven't passed any STRING(S) it outputs 'y' and passes it to "nl" which gives a number to every line. "nl" man page says:

"Write each FILE to standard output, with line numbers added.  With no FILE,
 or when FILE is -, read standard input."

Later on head -1000 limits the output to 1000 line and > writes the output into data.txt. Hope this will clarify.

Upvotes: 2

wanana
wanana

Reputation: 391

The output of the left command will be passed as the input of the command on the right of the |.

For you example, yes output unlimited number of y, and nl added rownumber to those y. Then the head command return the first 1000 lines of them.

The > is not part of a pipe. It's used for redirecting your output from STDOUT to a file.

Upvotes: 2

Related Questions