Reputation: 784
I have a file1
and a file2
.
I want to copy lines 3,4,5 of file1
into file2
, but after line 3.
I tried this, but it didn't work:
sed -i 3r<(sed '3,5!d' file1) file2
Any ideas? (I work with a macOS)
Example file1:
_line_1;
_line_2;
_line_3;
_line_4;
_line_5;
Example file2:
line1;
line2;
line3;
line4;
Example output
line1;
line2;
line3;
_line_3;
_line_4;
_line_5;
line4;
Upvotes: 0
Views: 1022
Reputation: 2471
With only ed if it's allowed
printf "%s\n" "kx" "r file1" "'x+3,'x+5m3" "'x+1,\$d" "w" | ed -s file2
Here the k command mark the last line of file2.
You must protect the $ to the shell.
Upvotes: 0
Reputation: 58430
This might work for you (GNU sed):
sed -n '3,5p' file1 | sed '3r /dev/stdin' file2
Turn off implicit printing in the first sed invocation and pipe the results (lines 3-5) from file1 to a second sed invocation that reads in these lines after line 3 of file2.
Upvotes: 1
Reputation: 52384
Using a mix of ed
and sed
:
printf "%s\n" '3r !sed -n 3,5p file1' w | ed -s file2
Inserts the results of the sed command after line 3 of file2. Said sed prints out lines 3 through 5 of file1. Then it saves the changed file2.
Upvotes: 0