1

In ed, one can retrieve the output of a command into the current buffer by using r !COMMAND. One can also write a set of lines to the input of a command by using 1,3w !COMMAND.

However, I cannot determine how to do both simultaneously.

r 1,3w !sort
1,3w !sort: No such file or directory

Is it possible to do this in ed?

merlin2011
  • 3,925

2 Answers2

1

The only way I found to do this requires using an external file to store the results temporarily.

$ cat input.txt 
13
5
29
22
45
64
17
20
69
91
$ ed input.txt 
29
1,3w !sort -n > temp.txt
8
1,3d
0r temp.txt
8
wq
29
merlin2011
  • 3,925
1

how to do both simultaneously

You can't write lines from the text buffer to some command stdin and read its stdout back in, replacing the original lines, in one go.
ed was clearly not designed to do that kind of stuff... Try vim.
That being said, you can always use some contortions like ed inside ed, e.g. open the file, delete those lines from the text buffer, process them via another ed invocation (which reads from the original file not from the current buffer) whose output you then read into the buffer before the original range of lines:

ed -s infile
5,8d
4r ! ed -s infile<<<$'5,8w !sort -n\nq'
,p
q
don_crissti
  • 82,805