I need to copy the content of a text file and paste it to another text file. The first text file has 10 lines of data and I need them to be copied to the second text file starting at line number 5 (for example). So in the second text file those data should written from line 5 to line 14. How can this be done? Thanks in advance. Consider me as a rookie regarding Linux.
Ubuntu – How to copy the content of a text file and paste it to another starting at a certain line
text processing
Related Question
- Ubuntu – How to selectively copy details from a file and paste them into a new file
- Ubuntu – How to repeat the content of a file n times
- Ubuntu – Split text file into several ones when pattern appears, with command line in linux
- Ubuntu – Copy specific text from a multiple line file and paste it in another file automatically using terminal
- Ubuntu – Find and report line numbers of empty lines in text file
- Extracting the values greater than a threshold in text file
Best Answer
The easiest tool here might be
sed
. To insertb.txt
intoa.txt
after the 5th line , you could write:sed
reads the file specifiied as argument (a.txt
) line by line. All lines get reproduced in the output just as they appeared in the input, unless they get altered by a command.The
5
is an address (line number) at which the following command shall be executed. The command we use isr
, which takes a file name as argument (hereb.txt
), reads it completely and inserts it into the output after the current line.As it stands above, this
sed
command line will only print the output to the terminal, without writing to any files. You can either redirect it to a new file (not any of the input files!) using Bash's output redirection:Or you can directly modify the outer input file
a.txt
by usingsed
's-i
(for "in-place") switch. If you write it as-i.bak
, it will make a backup copy of the original input file with the suffix.bak
first:An example: