How can I copy the content of a text file and paste it to another starting at a certain line?
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.
text-processing
add a comment |
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.
text-processing
add a comment |
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.
text-processing
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.
text-processing
text-processing
edited Aug 27 '17 at 21:31
Eliah Kagan
82.7k22227369
82.7k22227369
asked Aug 27 '17 at 21:19
G. PaschalisG. Paschalis
63117
63117
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
head
and tail
solution
Assume the source file is called ~/a
and the file to be inserted is called ~/b
. We'll put the merged file into ~/c
:
head -n 5 ~/a > ~/c
cat ~/b >> ~/c
tail --lines=+6 ~/a >> ~/c
- The path
~/
is short hand for your/home/user
directory name - head copies the first five lines of file
a
into newly created filec
- cat lists the contents of file
b
and appends it to filec
- tail appends file
a
starting at line 6 until the end to filec
After verification rename merged file
After verifying that file c
is merged correctly from files a
and b
we'll rename c
to a
using:
mv ~/c ~/a
mv
moves filec
into filea
. Data isn't physically moved. The file is simply renamed which saves time.
Thank you for your answer. It worked just fine. However is there another way to do this without using a third text file?
– G. Paschalis
Aug 27 '17 at 21:58
The third file can be renamed to the source file when done. I thought it best to be able to view the file first. I'll add the extra steps to the answer now.
– WinEunuuchs2Unix
Aug 27 '17 at 22:04
Or...(head -n 5 a.txt ; cat b.txt ; tail -n +6 a.txt) > c.txt
– JJoao
Sep 5 '17 at 16:26
1
@JJoao That is a nice one line summary :) For teaching purposes I prefer to use the one line per command method and nested if statements. However for copy and pasting into terminal I love the one liners!
– WinEunuuchs2Unix
Sep 5 '17 at 17:27
add a comment |
The easiest tool here might be sed
. To insert b.txt
into a.txt
after the 5th line , you could write:
sed '5r b.txt' a.txt
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 is r
, which takes a file name as argument (here b.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:
sed '5r b.txt' a.txt > c.txt
Or you can directly modify the outer input file a.txt
by using sed
'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:
sed -i '5r b.txt' a.txt
An example:
$ cat a.txt
January
February
March
April
May
October
November
December
$ cat b.txt
June
July
August
September
$ sed '5r b.txt' a.txt
January
February
March
April
May
June
July
August
September
October
November
December
add a comment |
(Reusing elegant example from @ByteCommander:)
awk '1; NR==5 {system("cat b.txt")}' a.txt
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f950432%2fhow-can-i-copy-the-content-of-a-text-file-and-paste-it-to-another-starting-at-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
head
and tail
solution
Assume the source file is called ~/a
and the file to be inserted is called ~/b
. We'll put the merged file into ~/c
:
head -n 5 ~/a > ~/c
cat ~/b >> ~/c
tail --lines=+6 ~/a >> ~/c
- The path
~/
is short hand for your/home/user
directory name - head copies the first five lines of file
a
into newly created filec
- cat lists the contents of file
b
and appends it to filec
- tail appends file
a
starting at line 6 until the end to filec
After verification rename merged file
After verifying that file c
is merged correctly from files a
and b
we'll rename c
to a
using:
mv ~/c ~/a
mv
moves filec
into filea
. Data isn't physically moved. The file is simply renamed which saves time.
Thank you for your answer. It worked just fine. However is there another way to do this without using a third text file?
– G. Paschalis
Aug 27 '17 at 21:58
The third file can be renamed to the source file when done. I thought it best to be able to view the file first. I'll add the extra steps to the answer now.
– WinEunuuchs2Unix
Aug 27 '17 at 22:04
Or...(head -n 5 a.txt ; cat b.txt ; tail -n +6 a.txt) > c.txt
– JJoao
Sep 5 '17 at 16:26
1
@JJoao That is a nice one line summary :) For teaching purposes I prefer to use the one line per command method and nested if statements. However for copy and pasting into terminal I love the one liners!
– WinEunuuchs2Unix
Sep 5 '17 at 17:27
add a comment |
head
and tail
solution
Assume the source file is called ~/a
and the file to be inserted is called ~/b
. We'll put the merged file into ~/c
:
head -n 5 ~/a > ~/c
cat ~/b >> ~/c
tail --lines=+6 ~/a >> ~/c
- The path
~/
is short hand for your/home/user
directory name - head copies the first five lines of file
a
into newly created filec
- cat lists the contents of file
b
and appends it to filec
- tail appends file
a
starting at line 6 until the end to filec
After verification rename merged file
After verifying that file c
is merged correctly from files a
and b
we'll rename c
to a
using:
mv ~/c ~/a
mv
moves filec
into filea
. Data isn't physically moved. The file is simply renamed which saves time.
Thank you for your answer. It worked just fine. However is there another way to do this without using a third text file?
– G. Paschalis
Aug 27 '17 at 21:58
The third file can be renamed to the source file when done. I thought it best to be able to view the file first. I'll add the extra steps to the answer now.
– WinEunuuchs2Unix
Aug 27 '17 at 22:04
Or...(head -n 5 a.txt ; cat b.txt ; tail -n +6 a.txt) > c.txt
– JJoao
Sep 5 '17 at 16:26
1
@JJoao That is a nice one line summary :) For teaching purposes I prefer to use the one line per command method and nested if statements. However for copy and pasting into terminal I love the one liners!
– WinEunuuchs2Unix
Sep 5 '17 at 17:27
add a comment |
head
and tail
solution
Assume the source file is called ~/a
and the file to be inserted is called ~/b
. We'll put the merged file into ~/c
:
head -n 5 ~/a > ~/c
cat ~/b >> ~/c
tail --lines=+6 ~/a >> ~/c
- The path
~/
is short hand for your/home/user
directory name - head copies the first five lines of file
a
into newly created filec
- cat lists the contents of file
b
and appends it to filec
- tail appends file
a
starting at line 6 until the end to filec
After verification rename merged file
After verifying that file c
is merged correctly from files a
and b
we'll rename c
to a
using:
mv ~/c ~/a
mv
moves filec
into filea
. Data isn't physically moved. The file is simply renamed which saves time.
head
and tail
solution
Assume the source file is called ~/a
and the file to be inserted is called ~/b
. We'll put the merged file into ~/c
:
head -n 5 ~/a > ~/c
cat ~/b >> ~/c
tail --lines=+6 ~/a >> ~/c
- The path
~/
is short hand for your/home/user
directory name - head copies the first five lines of file
a
into newly created filec
- cat lists the contents of file
b
and appends it to filec
- tail appends file
a
starting at line 6 until the end to filec
After verification rename merged file
After verifying that file c
is merged correctly from files a
and b
we'll rename c
to a
using:
mv ~/c ~/a
mv
moves filec
into filea
. Data isn't physically moved. The file is simply renamed which saves time.
edited Aug 27 '17 at 22:13
answered Aug 27 '17 at 21:30
WinEunuuchs2UnixWinEunuuchs2Unix
46.6k1190182
46.6k1190182
Thank you for your answer. It worked just fine. However is there another way to do this without using a third text file?
– G. Paschalis
Aug 27 '17 at 21:58
The third file can be renamed to the source file when done. I thought it best to be able to view the file first. I'll add the extra steps to the answer now.
– WinEunuuchs2Unix
Aug 27 '17 at 22:04
Or...(head -n 5 a.txt ; cat b.txt ; tail -n +6 a.txt) > c.txt
– JJoao
Sep 5 '17 at 16:26
1
@JJoao That is a nice one line summary :) For teaching purposes I prefer to use the one line per command method and nested if statements. However for copy and pasting into terminal I love the one liners!
– WinEunuuchs2Unix
Sep 5 '17 at 17:27
add a comment |
Thank you for your answer. It worked just fine. However is there another way to do this without using a third text file?
– G. Paschalis
Aug 27 '17 at 21:58
The third file can be renamed to the source file when done. I thought it best to be able to view the file first. I'll add the extra steps to the answer now.
– WinEunuuchs2Unix
Aug 27 '17 at 22:04
Or...(head -n 5 a.txt ; cat b.txt ; tail -n +6 a.txt) > c.txt
– JJoao
Sep 5 '17 at 16:26
1
@JJoao That is a nice one line summary :) For teaching purposes I prefer to use the one line per command method and nested if statements. However for copy and pasting into terminal I love the one liners!
– WinEunuuchs2Unix
Sep 5 '17 at 17:27
Thank you for your answer. It worked just fine. However is there another way to do this without using a third text file?
– G. Paschalis
Aug 27 '17 at 21:58
Thank you for your answer. It worked just fine. However is there another way to do this without using a third text file?
– G. Paschalis
Aug 27 '17 at 21:58
The third file can be renamed to the source file when done. I thought it best to be able to view the file first. I'll add the extra steps to the answer now.
– WinEunuuchs2Unix
Aug 27 '17 at 22:04
The third file can be renamed to the source file when done. I thought it best to be able to view the file first. I'll add the extra steps to the answer now.
– WinEunuuchs2Unix
Aug 27 '17 at 22:04
Or...
(head -n 5 a.txt ; cat b.txt ; tail -n +6 a.txt) > c.txt
– JJoao
Sep 5 '17 at 16:26
Or...
(head -n 5 a.txt ; cat b.txt ; tail -n +6 a.txt) > c.txt
– JJoao
Sep 5 '17 at 16:26
1
1
@JJoao That is a nice one line summary :) For teaching purposes I prefer to use the one line per command method and nested if statements. However for copy and pasting into terminal I love the one liners!
– WinEunuuchs2Unix
Sep 5 '17 at 17:27
@JJoao That is a nice one line summary :) For teaching purposes I prefer to use the one line per command method and nested if statements. However for copy and pasting into terminal I love the one liners!
– WinEunuuchs2Unix
Sep 5 '17 at 17:27
add a comment |
The easiest tool here might be sed
. To insert b.txt
into a.txt
after the 5th line , you could write:
sed '5r b.txt' a.txt
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 is r
, which takes a file name as argument (here b.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:
sed '5r b.txt' a.txt > c.txt
Or you can directly modify the outer input file a.txt
by using sed
'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:
sed -i '5r b.txt' a.txt
An example:
$ cat a.txt
January
February
March
April
May
October
November
December
$ cat b.txt
June
July
August
September
$ sed '5r b.txt' a.txt
January
February
March
April
May
June
July
August
September
October
November
December
add a comment |
The easiest tool here might be sed
. To insert b.txt
into a.txt
after the 5th line , you could write:
sed '5r b.txt' a.txt
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 is r
, which takes a file name as argument (here b.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:
sed '5r b.txt' a.txt > c.txt
Or you can directly modify the outer input file a.txt
by using sed
'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:
sed -i '5r b.txt' a.txt
An example:
$ cat a.txt
January
February
March
April
May
October
November
December
$ cat b.txt
June
July
August
September
$ sed '5r b.txt' a.txt
January
February
March
April
May
June
July
August
September
October
November
December
add a comment |
The easiest tool here might be sed
. To insert b.txt
into a.txt
after the 5th line , you could write:
sed '5r b.txt' a.txt
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 is r
, which takes a file name as argument (here b.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:
sed '5r b.txt' a.txt > c.txt
Or you can directly modify the outer input file a.txt
by using sed
'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:
sed -i '5r b.txt' a.txt
An example:
$ cat a.txt
January
February
March
April
May
October
November
December
$ cat b.txt
June
July
August
September
$ sed '5r b.txt' a.txt
January
February
March
April
May
June
July
August
September
October
November
December
The easiest tool here might be sed
. To insert b.txt
into a.txt
after the 5th line , you could write:
sed '5r b.txt' a.txt
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 is r
, which takes a file name as argument (here b.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:
sed '5r b.txt' a.txt > c.txt
Or you can directly modify the outer input file a.txt
by using sed
'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:
sed -i '5r b.txt' a.txt
An example:
$ cat a.txt
January
February
March
April
May
October
November
December
$ cat b.txt
June
July
August
September
$ sed '5r b.txt' a.txt
January
February
March
April
May
June
July
August
September
October
November
December
answered Aug 27 '17 at 22:59
Byte CommanderByte Commander
65.8k27180305
65.8k27180305
add a comment |
add a comment |
(Reusing elegant example from @ByteCommander:)
awk '1; NR==5 {system("cat b.txt")}' a.txt
add a comment |
(Reusing elegant example from @ByteCommander:)
awk '1; NR==5 {system("cat b.txt")}' a.txt
add a comment |
(Reusing elegant example from @ByteCommander:)
awk '1; NR==5 {system("cat b.txt")}' a.txt
(Reusing elegant example from @ByteCommander:)
awk '1; NR==5 {system("cat b.txt")}' a.txt
answered Sep 5 '17 at 16:24
JJoaoJJoao
1,40069
1,40069
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f950432%2fhow-can-i-copy-the-content-of-a-text-file-and-paste-it-to-another-starting-at-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown