samedi 31 janvier 2015

Pipes, how do data flow in a pipeline?


I don't understand how the data flows in the pipeline and hope someone could clarify what is going on there.


I thought a pipeline of commands processes files (text, arrays of strings) in line by line manner. (If each command itself works line by line.) Each line of text passes through the pipeline, commands don't wait for the previous to finish processing whole input.


But it seems it is not so.


Here is a test example. There are some lines of text. I uppercase them and repeat each line twice. I do so with cat text | tr '[:lower:]' '[:upper:]' | sed 'p'.


To follow the process we can run it "interactively" -- skip the input filename in cat. Each part of the pipeline runs line by line:



$ cat | tr '[:lower:]' '[:upper:]'
alkjsd
ALKJSD
sdkj
SDKJ
$ cat | sed 'p'
line1
line1
line1
line 2
line 2
line 2


But the complete pipeline does wait for me to finish the input with EOF and only then prints the result:



$ cat | tr '[:lower:]' '[:upper:]' | sed 'p'
I am writing...
keep writing...
now ctrl-D
I AM WRITING...
I AM WRITING...
KEEP WRITING...
KEEP WRITING...
NOW CTRL-D
NOW CTRL-D


Is it supposed to be so? Why isn't it line-by-line?



Aucun commentaire:

Enregistrer un commentaire