Skip to main content
7 of 12
added 15 characters in body
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

Well, after monkeying around with it, this is what I'd use:

 awk '{ print $0; system("sleep 0") }' edscript | tee /dev/tty | ed

or

awk '{ print $0 >"/dev/stderr"; print $0 | "ed"; system("sleep 0") }' edscript

The system("sleep 0") is there just to send each line as a distinct output to the pipe. You could equally use something else, like system("date >/dev/null") or system("echo BREAK >/dev/null").


Another (more complicated) solution that would work for your trivial case where the only reply from ed is a result of ,n or ,p commands (it should also work for any other substitutions, deletions, line splitting/removal but not for line/pattern/range addressing or printing1):
Use csplit to split your edscript on each ,n or ,p

csplit -f edscript. edscript /^,[pn]$/+1 {*}

you could then use the pieces like this:

for i in {00..02}; do printf '%s\n' "! cat edscript.$i" "$(cat edscript.$i)"; done

to pass the following to ed:

! cat tt.00
a
hello
world
.
,n
! cat tt.01
,s,o,O,g
,n
! cat tt.02
Q

that is for each piece, ed will receive the following input: the first line is !cat piece - ed will have the shell actually run the command so the content of piece is displayed; after that, ed gets the actual content of piece, i.e. the commands that it has to execute.
So running:

for i in {00..02}; do printf '%s\n' "! cat edscript.$i" "$(cat edscript.$i)"; done | ed -s

produces the following output:

a
hello
world
.
,n
1   hello
2   world
,s,o,O,g
,n
1   hellO
2   wOrld
Q

For other cases with different number of pieces, replace 02 inside {00..02} with whatever number your last piece is.


1As I said, this solution assumes that ed is silent unless running ,n or ,p so it won't work as expected for line/pattern/range addressing/printing in a script, e.g. if I have:

a
hello
world
my
name is
.
1,3p
/wo/

on the last two commands ed will display the addressed lines:

1,3p
hello
world
my
/wo/
world

but since my solution doesn't split edscript on those particular markers I will not get the expected output right after each command that was printed by !cat piece (I will get it only later when the actual content is executed).
Theoretically, if you knew which lines from your edscript produce some output, you would split edscript right after each of those lines and then yes, each line/command will have its corresponding output right below it.

don_crissti
  • 85.6k
  • 31
  • 234
  • 262