Linux Shell/SED query : To copy multiple lines from one file to another

mathrisk

.: deleted :.
Level G
The problem at hand is, copying multiple lines from one file and paste at some specific line# on the other.

Say File1
Line 1
Line 2
Line 3
Line 4

File 2
Line A
Line B
Line C
Line D

I need to copy two lines from file1 and paste them before line 3 on file2
Output File2 looks like

Line A
Line B
Line 2
Line 3
Line C
Line D

Can SED be used to do that? How?
Though I am well versed in vim, never used sed before. But since my number of lines are huge (500+), it becomes tough to do so in vim.
Or is there any other way? (than visual mode in vim)


TIA.
 
Last edited:
You can either run a loop to create new file by reading from both file or
Use awk to control line no from NR or using sed by pattern space and keep checking line no
 
Was this one time thing ?

Not exactly. This time I had around 10/12 files to edit (copy hundreds of lines from other files).
But this may again return in the next dev cycle.
(The development is of something similar to some existing functionality. Copy paste the existing, change what needed.)
 
Not exactly. This time I had around 10/12 files to edit (copy hundreds of lines from other files).
But this may again return in the next dev cycle.
(The development is of something similar to some existing functionality. Copy paste the existing, change what needed.)

What's the pattern you want to copy lines from one file to another ? Paste after 2 lines ? Why don't you just run a loop and in keep track of lines its easy with NR
 
Back
Top