Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sed blocking?

I had the impression sed wasn't blocking, because when I do say:

iostat | sed

sed processes the data as it arrives, but when I do

iostat | sed | netcat

Then sed blocks netcat.

Am I right?

like image 887
Robert Gould Avatar asked Feb 23 '09 14:02

Robert Gould


People also ask

Does sed modify the original file?

By default sed does not overwrite the original file; it writes to stdout (hence the result can be redirected using the shell operator > as you showed).

Does sed work line by line?

The sed command, short for stream editor, performs editing operations on text coming from standard input or a file. sed edits line-by-line and in a non-interactive way.

What does R mean in sed?

r is used to read a file and append it at the current point. The point in your example is the address /EOF/ which means this script will find the line containing EOF and then append the file specified by $thingToAdd after that point. Then it will process the rest of the file.

What is I flag in sed?

The I flag allows to match a pattern case insensitively. Usually i is used for such purposes, grep -i for example. But i is a command (discussed in append, change, insert chapter) in sed , so /REGEXP/i cannot be used.


3 Answers

sed will work in buffered mode when it doesn't print to a terminal. This means that it will try to fill its internal buffer before doing any processing and output by default.

This is done to increase throughput, because normally in a pipe you don't care about the timing, but want as much data processed in a given time as possible.

Passing -u to sed will tell it to work unbuffered, therefore working the same way it works when output goes to a terminal.

like image 167
Joachim Sauer Avatar answered Oct 13 '22 06:10

Joachim Sauer


In addition to what @saua says, sed is at least line oriented, that it, it read a line, then operates on it so it will always be buffering at least one line. In addition, sed can work in multiline mode. If you are using a multiline pattern, then sed can't output it's current buffer until it knows that the pattern either doesn't apply or the pattern has been processed.

like image 42
tvanfosson Avatar answered Oct 13 '22 07:10

tvanfosson


I don't know if I understand the question right, but in your example, it should be like this:

  • sed waits for iostat to send data
  • iostat will wait for sed to process the data if sed is slow
  • netcat waits for sed to send data
  • sed will wait for netcat again, if it is slow

Other than that, sed shouldn't need to read all its input to produce output.

Do you observe any delays that cannot be explained by this and some small buffering?

like image 2
jpalecek Avatar answered Oct 13 '22 06:10

jpalecek