I need to write the output of a command to a specific line in a document. I can not just append it like so COMMAND | cat >> file
, I need it to be added between two lines without replacing one or the other. I'm sure you must be able to do this via sed.
Asked
Active
Viewed 1,964 times
0

user3094719
- 297
- 4
- 16
-
1Possible duplicate of http://stackoverflow.com/questions/8971314/how-to-insert-a-line-in-a-file-between-two-blocks-of-known-lines-if-not-already – AlG Jan 13 '15 at 15:51
-
possible duplicate of [Add text between two patterns in File using sed command](http://stackoverflow.com/questions/16836306/add-text-between-two-patterns-in-file-using-sed-command) – buydadip Jan 13 '15 at 16:39
2 Answers
2
The following solution works when the output of COMMAND
is only 1 line (inserting to line 4):
COMMAND | sed -i "4i \`cat` FILE"

user3094719
- 297
- 4
- 16
1
Use that command:
command | sed -i '3r /dev/stdin' file
That inserts text after the 3rd line and reads from stdin (all output from command
).

chaos
- 8,162
- 3
- 33
- 40