How to copy the rest of lines of a file to another file
I have the string xyz
which is a line in file1.txt
, I want to copy all the lines after xyz
in file1.txt
to a new file file2.txt
. How can I achieve this?
I know about cat
command. But how to specify the starting line?
files grep cat file-copy file-transfer
add a comment |
I have the string xyz
which is a line in file1.txt
, I want to copy all the lines after xyz
in file1.txt
to a new file file2.txt
. How can I achieve this?
I know about cat
command. But how to specify the starting line?
files grep cat file-copy file-transfer
2
Do you want to include thatxyz
line or exclude it from being copied ? Also, what happens if you have multiple lines matchingxyz
?
– don_crissti
6 hours ago
2
Possible duplicate of How to print all lines after a match up to the end of the file?
– Kusalananda
6 hours ago
add a comment |
I have the string xyz
which is a line in file1.txt
, I want to copy all the lines after xyz
in file1.txt
to a new file file2.txt
. How can I achieve this?
I know about cat
command. But how to specify the starting line?
files grep cat file-copy file-transfer
I have the string xyz
which is a line in file1.txt
, I want to copy all the lines after xyz
in file1.txt
to a new file file2.txt
. How can I achieve this?
I know about cat
command. But how to specify the starting line?
files grep cat file-copy file-transfer
files grep cat file-copy file-transfer
asked 6 hours ago
user9371654user9371654
30617
30617
2
Do you want to include thatxyz
line or exclude it from being copied ? Also, what happens if you have multiple lines matchingxyz
?
– don_crissti
6 hours ago
2
Possible duplicate of How to print all lines after a match up to the end of the file?
– Kusalananda
6 hours ago
add a comment |
2
Do you want to include thatxyz
line or exclude it from being copied ? Also, what happens if you have multiple lines matchingxyz
?
– don_crissti
6 hours ago
2
Possible duplicate of How to print all lines after a match up to the end of the file?
– Kusalananda
6 hours ago
2
2
Do you want to include that
xyz
line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz
?– don_crissti
6 hours ago
Do you want to include that
xyz
line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz
?– don_crissti
6 hours ago
2
2
Possible duplicate of How to print all lines after a match up to the end of the file?
– Kusalananda
6 hours ago
Possible duplicate of How to print all lines after a match up to the end of the file?
– Kusalananda
6 hours ago
add a comment |
4 Answers
4
active
oldest
votes
Using GNU sed
To copy all lines after xyz
, try:
sed '0,/xyz/d' file1.txt >file2.txt
1,/xyz/
specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz
. d
tells sed to delete those lines.
Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt
but this only works if the first appearance of xyz
is in the second line or later. (Hat tip: kusalananda.)
Another approach, as suggested by don_crissti, should work for all sed:
{ printf %s\n; cat file1.txt; } | sed '1,/xyz/d' >file2.txt
Example
Consider this test file:
$ cat file1.txt
a
b
xyz
c
d
Run our command:
$ sed '1,/xyz/d' file1.txt >file2.txt
$ cat file2.txt
c
d
Using awk
The same logic can used with awk:
awk 'NR==1,/xyz/{next} 1' file1.txt >file2.txt
NR==1,/xyz/{next}
tells awk to skip over all lines from the first (NR==1
) to the first line matching the regex xyz
. 1
tells awk to print any remaining lines.
@John1024 Whatsed
are you using there?
– Kusalananda
5 hours ago
@Kusalanandased --version
returns(GNU sed) 4.4
.
– John1024
5 hours ago
add a comment |
With ed
:
ed -s file.txt <<< $'/xyz/+1,$w file2.txt'
This sends one (ranged) command to ed
: from the line after (+1
) the one containing xyz
until the end of the file ($
), w
rite those lines to file2.txt
.
add a comment |
There is also csplit :
csplit -s file1.txt %xyz%1
add a comment |
$ sed -n '/xyz/,$p' file.txt > file2.txt
With -n
we prevent sed
to print every line. With $
means end of file end p
stands for print line. So /xyz/$p
means: If a line matches xyz
print it until the end of the file.
This would also print the line matchingxyz
, not from the line after.
– Kusalananda
6 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f505388%2fhow-to-copy-the-rest-of-lines-of-a-file-to-another-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using GNU sed
To copy all lines after xyz
, try:
sed '0,/xyz/d' file1.txt >file2.txt
1,/xyz/
specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz
. d
tells sed to delete those lines.
Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt
but this only works if the first appearance of xyz
is in the second line or later. (Hat tip: kusalananda.)
Another approach, as suggested by don_crissti, should work for all sed:
{ printf %s\n; cat file1.txt; } | sed '1,/xyz/d' >file2.txt
Example
Consider this test file:
$ cat file1.txt
a
b
xyz
c
d
Run our command:
$ sed '1,/xyz/d' file1.txt >file2.txt
$ cat file2.txt
c
d
Using awk
The same logic can used with awk:
awk 'NR==1,/xyz/{next} 1' file1.txt >file2.txt
NR==1,/xyz/{next}
tells awk to skip over all lines from the first (NR==1
) to the first line matching the regex xyz
. 1
tells awk to print any remaining lines.
@John1024 Whatsed
are you using there?
– Kusalananda
5 hours ago
@Kusalanandased --version
returns(GNU sed) 4.4
.
– John1024
5 hours ago
add a comment |
Using GNU sed
To copy all lines after xyz
, try:
sed '0,/xyz/d' file1.txt >file2.txt
1,/xyz/
specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz
. d
tells sed to delete those lines.
Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt
but this only works if the first appearance of xyz
is in the second line or later. (Hat tip: kusalananda.)
Another approach, as suggested by don_crissti, should work for all sed:
{ printf %s\n; cat file1.txt; } | sed '1,/xyz/d' >file2.txt
Example
Consider this test file:
$ cat file1.txt
a
b
xyz
c
d
Run our command:
$ sed '1,/xyz/d' file1.txt >file2.txt
$ cat file2.txt
c
d
Using awk
The same logic can used with awk:
awk 'NR==1,/xyz/{next} 1' file1.txt >file2.txt
NR==1,/xyz/{next}
tells awk to skip over all lines from the first (NR==1
) to the first line matching the regex xyz
. 1
tells awk to print any remaining lines.
@John1024 Whatsed
are you using there?
– Kusalananda
5 hours ago
@Kusalanandased --version
returns(GNU sed) 4.4
.
– John1024
5 hours ago
add a comment |
Using GNU sed
To copy all lines after xyz
, try:
sed '0,/xyz/d' file1.txt >file2.txt
1,/xyz/
specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz
. d
tells sed to delete those lines.
Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt
but this only works if the first appearance of xyz
is in the second line or later. (Hat tip: kusalananda.)
Another approach, as suggested by don_crissti, should work for all sed:
{ printf %s\n; cat file1.txt; } | sed '1,/xyz/d' >file2.txt
Example
Consider this test file:
$ cat file1.txt
a
b
xyz
c
d
Run our command:
$ sed '1,/xyz/d' file1.txt >file2.txt
$ cat file2.txt
c
d
Using awk
The same logic can used with awk:
awk 'NR==1,/xyz/{next} 1' file1.txt >file2.txt
NR==1,/xyz/{next}
tells awk to skip over all lines from the first (NR==1
) to the first line matching the regex xyz
. 1
tells awk to print any remaining lines.
Using GNU sed
To copy all lines after xyz
, try:
sed '0,/xyz/d' file1.txt >file2.txt
1,/xyz/
specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz
. d
tells sed to delete those lines.
Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt
but this only works if the first appearance of xyz
is in the second line or later. (Hat tip: kusalananda.)
Another approach, as suggested by don_crissti, should work for all sed:
{ printf %s\n; cat file1.txt; } | sed '1,/xyz/d' >file2.txt
Example
Consider this test file:
$ cat file1.txt
a
b
xyz
c
d
Run our command:
$ sed '1,/xyz/d' file1.txt >file2.txt
$ cat file2.txt
c
d
Using awk
The same logic can used with awk:
awk 'NR==1,/xyz/{next} 1' file1.txt >file2.txt
NR==1,/xyz/{next}
tells awk to skip over all lines from the first (NR==1
) to the first line matching the regex xyz
. 1
tells awk to print any remaining lines.
edited 4 hours ago
answered 6 hours ago
John1024John1024
47.5k5110125
47.5k5110125
@John1024 Whatsed
are you using there?
– Kusalananda
5 hours ago
@Kusalanandased --version
returns(GNU sed) 4.4
.
– John1024
5 hours ago
add a comment |
@John1024 Whatsed
are you using there?
– Kusalananda
5 hours ago
@Kusalanandased --version
returns(GNU sed) 4.4
.
– John1024
5 hours ago
@John1024 What
sed
are you using there?– Kusalananda
5 hours ago
@John1024 What
sed
are you using there?– Kusalananda
5 hours ago
@Kusalananda
sed --version
returns (GNU sed) 4.4
.– John1024
5 hours ago
@Kusalananda
sed --version
returns (GNU sed) 4.4
.– John1024
5 hours ago
add a comment |
With ed
:
ed -s file.txt <<< $'/xyz/+1,$w file2.txt'
This sends one (ranged) command to ed
: from the line after (+1
) the one containing xyz
until the end of the file ($
), w
rite those lines to file2.txt
.
add a comment |
With ed
:
ed -s file.txt <<< $'/xyz/+1,$w file2.txt'
This sends one (ranged) command to ed
: from the line after (+1
) the one containing xyz
until the end of the file ($
), w
rite those lines to file2.txt
.
add a comment |
With ed
:
ed -s file.txt <<< $'/xyz/+1,$w file2.txt'
This sends one (ranged) command to ed
: from the line after (+1
) the one containing xyz
until the end of the file ($
), w
rite those lines to file2.txt
.
With ed
:
ed -s file.txt <<< $'/xyz/+1,$w file2.txt'
This sends one (ranged) command to ed
: from the line after (+1
) the one containing xyz
until the end of the file ($
), w
rite those lines to file2.txt
.
answered 5 hours ago
Jeff SchallerJeff Schaller
43.2k1159138
43.2k1159138
add a comment |
add a comment |
There is also csplit :
csplit -s file1.txt %xyz%1
add a comment |
There is also csplit :
csplit -s file1.txt %xyz%1
add a comment |
There is also csplit :
csplit -s file1.txt %xyz%1
There is also csplit :
csplit -s file1.txt %xyz%1
answered 5 hours ago
ctac_ctac_
1,4321210
1,4321210
add a comment |
add a comment |
$ sed -n '/xyz/,$p' file.txt > file2.txt
With -n
we prevent sed
to print every line. With $
means end of file end p
stands for print line. So /xyz/$p
means: If a line matches xyz
print it until the end of the file.
This would also print the line matchingxyz
, not from the line after.
– Kusalananda
6 hours ago
add a comment |
$ sed -n '/xyz/,$p' file.txt > file2.txt
With -n
we prevent sed
to print every line. With $
means end of file end p
stands for print line. So /xyz/$p
means: If a line matches xyz
print it until the end of the file.
This would also print the line matchingxyz
, not from the line after.
– Kusalananda
6 hours ago
add a comment |
$ sed -n '/xyz/,$p' file.txt > file2.txt
With -n
we prevent sed
to print every line. With $
means end of file end p
stands for print line. So /xyz/$p
means: If a line matches xyz
print it until the end of the file.
$ sed -n '/xyz/,$p' file.txt > file2.txt
With -n
we prevent sed
to print every line. With $
means end of file end p
stands for print line. So /xyz/$p
means: If a line matches xyz
print it until the end of the file.
answered 6 hours ago
finswimmerfinswimmer
72917
72917
This would also print the line matchingxyz
, not from the line after.
– Kusalananda
6 hours ago
add a comment |
This would also print the line matchingxyz
, not from the line after.
– Kusalananda
6 hours ago
This would also print the line matching
xyz
, not from the line after.– Kusalananda
6 hours ago
This would also print the line matching
xyz
, not from the line after.– Kusalananda
6 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f505388%2fhow-to-copy-the-rest-of-lines-of-a-file-to-another-file%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
2
Do you want to include that
xyz
line or exclude it from being copied ? Also, what happens if you have multiple lines matchingxyz
?– don_crissti
6 hours ago
2
Possible duplicate of How to print all lines after a match up to the end of the file?
– Kusalananda
6 hours ago