Skip to content
Latchkey

sed in Pipes: Reading from stdin

With no file argument sed reads standard input, which is how it fits between other commands in a pipeline.

sed is built for pipes. Give it a program and no file, and it edits whatever flows in on stdin and writes the result to stdout.

What it does

When you do not name a file, sed reads from stdin until end of input. This lets it transform the output of another command before the next stage. The explicit argument - also means stdin and is useful to mix stdin with named files.

Common usage

Terminal
# edit the output of another command
curl -s https://example.com/v.txt | sed 's/^v//'

# trim leading whitespace from each line
cat file | sed 's/^[[:space:]]*//'

# explicit stdin alongside a file
sed 's/a/b/' - header.txt < body.txt

Options

FormWhat it does
cmd | sed '...'Edit the piped stream from stdin
-Explicit name for standard input
-u (GNU)Unbuffered: flush each line, useful for live logs
-l (BSD)Line-buffered output on BSD sed
< fileRedirect a file into sed as stdin

In CI

For tailing a live log through sed, add -u on GNU sed (or -l on BSD) so output is not held in a block buffer and appears in the job log immediately. In a non-interactive pipeline, normal buffering is usually fine and faster.

Common errors in CI

If sed seems to hang with no file, it is waiting on stdin; that happens when the upstream command produced no output or the pipe was not connected. A useless use of cat such as cat file | sed works but spawns an extra process; sed '...' file is equivalent and cleaner.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →