The ed text editor does not allow you to filter lines of the buffer
through external commands using the ! command. What you can do is to
read the output of a command and append the output to a specific
address within the text. The general solution is to use a temporary file to hold the output of the command, and then read that file into
the buffer at the desired location.
Here is how you can do it:
First, write the lines you want to filter to a temporary file. For example, if you wish to filter lines 1 to 5, you can do this from within
ed:1,5w /tmp/tempfile.txtNext, delete the lines you want to filter from the buffer:
1,5dNow apply the external command to the temporary file and redirect and read the output to the location you want to insert the output. Finally, remove the temporary file:
1r !sort /tmp/tempfile.txt !rm /tmp/sortedfile.txt
Of course, this might be a bit dumbersome, but it should work with any external command. If you find yourself doing this more often, it might be worthwhile to writ a short shell script to automate the process.
Finally, if you use GNU ed, you can use the addressing commands together with the ! command to achieve a similar effect without needing a temporary file. For example, to filter lines 1 to 5 through sort, you can do:
1,5!sort
to achieve the same effect.