How can I provide stdin to ed, which need a filename?


Need some unix shell basic here:

For command that I see no “-” target in , say ed:

print '%-2p\nq' | ed -s FILE

Can I provide a stream from stdout of some cmd, rather than FILE name, as the data to be processed:

SomeCMD | ed -s SOMETHING_MAGICAL <<< 'print '%-2p\nq'

Is is possible?


ed reads its commands from stdin, so if your file is also on stdin, how do you work?

In fact, you can feed file input over stdin, if you concatenate its output with a single line


at the beginning, to start writing in the data, then append a single . to end the input, followed by any commands. You can even output the results to stdout. Do remember that it will break if there is a line in the file with nothing but a single . in it.

So if a file input.file contains this:

First line
Second line
Third line

And a file commands.list contains this:

1,$w /dev/stdout

Then this command line…

echo i | cat - input.file commands.list | ed -s

Will output this:

Second line
Third line

Dare I say tadaaaaa! ?

Note: you can probably protect against the case of single . lines in the file by piping the file through a filter that escapes any such lines and then unescaping them again with ed commands. I leave that to your ingenuity.

Another note: you really should use sed for this, but I couldn’t let the it can’t be done comments go by.

Answered By – itsbruce

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published