Reputation: 1115
I know sed
can append text in a few different ways:
to append the string NEW_TEXT
:
sed -i '/PATTERN/ a NEW_TEXT' file
to append contents of file file_with_new_text
:
sed -i '/PATTERN/ r file_with_new_text' file
However, is there anyway to append text piped to sed
via stdout? I am hoping to obtain the equivalent of:
# XXX,YYY,ZZZ are line numbers
# The following appends lines XXX-YYY in file1
# after line ZZZ in file2.
sed 'XXX,YYY p' file1 > /tmp/file
sed -i 'ZZZ r /tmp/file' file2
without having to use a temporary file. Is this possible?
Upvotes: 6
Views: 4150
Reputation: 785541
You can use redirection operator with r
command:
sed -i '/ZZZ/r '<(sed -n '1p' file1) file2
Upvotes: 4
Reputation: 123560
You can read from /dev/stdin
. Clearly, this will only work once per sed process.
Example:
$ cat file1
this is text from file1
etc
$ cat file2
Hello world from file2
$ sed -n '1p' file1 | sed -i '$ r /dev/stdin' file2
$ cat file2
Hello world from file2
this is text from file1
Upvotes: 9