Insert another file's content to current file - VI Editor

Easy way to insert another file's content to current file using VI is,

> Open the file

# vi myfile.txt

:r myanotherfile.txt

Here we can use r or read to load myanotherfile.txt on myfile.txt, Now the myanotherfile.txt's contents placed immediately after the current line.

If you wish to place the contents in particular line ?

Okay, If you wish to place the myanotherfile.txt's contents on myfile.txt at 5th line,

> Open the file

# vi myfile.txt

:4r myanotherfile.txt

so that the contents of myanotherfile.txt were placed immediately after the 4th line ( obviously on 5th line as we required).

You can do this without opening the myfile.txt:

Below command to insert myanotherfile.txt's contents on myfile.txt:

# sed -i 'r myanotherfile.txt' myfile.txt

now myanotherfile.txt's contents on myfile.txt at second line( I hop we already know why it placed on second line ! ),

If you wish to place this on 5th line, then:

# sed -i '4r myanotherfile.txt' myfile.txt

If you would like to insert a word on particular line ?

yes we can, the same way:

# sed -i '4a NEW LINE ADDED' myfile.txt

You can see the 'NEW LINE ADDED' on myfile.txt at 5th line.

cool...

Comments

Popular posts from this blog

Resolved: DOCKER: Error response from daemon: Could not attach to network / rpc error: code = 7 desc = network not manually attachable.

yum failed 6 times. Cannot continue!

How to echo colored text in linux shell script: