Split a file into two
Clash Royale CLAN TAG#URR8PPP
up vote
13
down vote
favorite
I have a big file and need to split into two files. Suppose in the first file the 1000 lines should be selected and put into another file and delete those lines in the first file.
I tried using split
but it is creating multiple chunks.
text-processing awk split csplit
add a comment |Â
up vote
13
down vote
favorite
I have a big file and need to split into two files. Suppose in the first file the 1000 lines should be selected and put into another file and delete those lines in the first file.
I tried using split
but it is creating multiple chunks.
text-processing awk split csplit
Did you checksplit --help
?
â Braiam
Oct 21 '14 at 16:01
Yes i have checked it, but is creating multiple files which doesn't need to me.
â Aravind
Oct 21 '14 at 16:02
add a comment |Â
up vote
13
down vote
favorite
up vote
13
down vote
favorite
I have a big file and need to split into two files. Suppose in the first file the 1000 lines should be selected and put into another file and delete those lines in the first file.
I tried using split
but it is creating multiple chunks.
text-processing awk split csplit
I have a big file and need to split into two files. Suppose in the first file the 1000 lines should be selected and put into another file and delete those lines in the first file.
I tried using split
but it is creating multiple chunks.
text-processing awk split csplit
text-processing awk split csplit
edited May 6 '16 at 17:01
don_crissti
47.3k15125155
47.3k15125155
asked Oct 21 '14 at 15:50
Aravind
51761332
51761332
Did you checksplit --help
?
â Braiam
Oct 21 '14 at 16:01
Yes i have checked it, but is creating multiple files which doesn't need to me.
â Aravind
Oct 21 '14 at 16:02
add a comment |Â
Did you checksplit --help
?
â Braiam
Oct 21 '14 at 16:01
Yes i have checked it, but is creating multiple files which doesn't need to me.
â Aravind
Oct 21 '14 at 16:02
Did you check
split --help
?â Braiam
Oct 21 '14 at 16:01
Did you check
split --help
?â Braiam
Oct 21 '14 at 16:01
Yes i have checked it, but is creating multiple files which doesn't need to me.
â Aravind
Oct 21 '14 at 16:02
Yes i have checked it, but is creating multiple files which doesn't need to me.
â Aravind
Oct 21 '14 at 16:02
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
19
down vote
accepted
The easiest way is probably to use head
and tail
:
$ head -n 1000 input-file > output1
$ tail -n +1001 input-file > output2
That will put the first 1000 lines from input-file
into output1
, and all lines from 1001 till the end in output2
add a comment |Â
up vote
8
down vote
I think that split
is you best approach.
Try using the -l xxxx
option, where xxxx is the number of lines you want in each file (default is 1000).
You can use the -n yy
option if you are more concerned about the amount of files created. Use -n 2
will split your file in only 2 parts, no matter the amount of lines in each file.
You can count the amount of lines in your file with wc -l filename
. This is the 'wordcount' command with the lines option.
References
man split
man wc
1
This is how to split into a bunch of files with a fixed number of lines, or how to split evenly into a fixed number of files. Is there a way to split into one 1000-line file and one file with everything else? That's what he was asking for; I couldn't find it in the man page
â Michael Mrozekâ¦
Oct 21 '14 at 17:05
You´re correct Michael. I think I took a simplistic view on the question. You solution is the best one in this case. Another way would be to use the 'sed' command: sed -n 1,1000 originalfile > first_1000_lines. sed '1,1000d' originalfile > remaining_lines.
â Lucien Raven
Oct 21 '14 at 17:17
Of course you could dosplit -l 1000 bigfile && mv xaa piece1 && cat x?? > piece2 && rm x??
.
â G-Man
Oct 21 '14 at 23:40
add a comment |Â
up vote
6
down vote
This is a job for csplit
:
csplit -s infile 1001
will s
ilently split infile
, the first piece xx00
- up to but not including line 1001 and the second piece xx01
- the remaining lines.
You can play with the options if you need different output file names e.g. using -f
and specifying a prefix:
csplit -sf piece. infile 1001
produces two files named piece.00
and piece.01
With a smart head
you could also do something like:
head -n 1000 > 1st.out; cat > 2nd.out; < infile
Wow, it really is a job forcsplit
. Very nice. (I'm just reading through the list of POSIX commands and had enormous trouble wrapping my head around thecsplit
command's purpose at first. Turns out it's really really simple.) :)
â Wildcard
Nov 2 '16 at 5:38
add a comment |Â
up vote
4
down vote
A simple way to do what the question asks for, in one command:
awk ' if (NR <= 1000) print > "piece1"; else print > "piece2"; ' bigfile
or, for those of you who really hate to type long, intuitively comprehensible commands,
awk ' print > ((NR <= 1000) ? "piece1" : "piece2"); ' bigfile
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
19
down vote
accepted
The easiest way is probably to use head
and tail
:
$ head -n 1000 input-file > output1
$ tail -n +1001 input-file > output2
That will put the first 1000 lines from input-file
into output1
, and all lines from 1001 till the end in output2
add a comment |Â
up vote
19
down vote
accepted
The easiest way is probably to use head
and tail
:
$ head -n 1000 input-file > output1
$ tail -n +1001 input-file > output2
That will put the first 1000 lines from input-file
into output1
, and all lines from 1001 till the end in output2
add a comment |Â
up vote
19
down vote
accepted
up vote
19
down vote
accepted
The easiest way is probably to use head
and tail
:
$ head -n 1000 input-file > output1
$ tail -n +1001 input-file > output2
That will put the first 1000 lines from input-file
into output1
, and all lines from 1001 till the end in output2
The easiest way is probably to use head
and tail
:
$ head -n 1000 input-file > output1
$ tail -n +1001 input-file > output2
That will put the first 1000 lines from input-file
into output1
, and all lines from 1001 till the end in output2
answered Oct 21 '14 at 16:11
Michael Mrozekâ¦
58.7k27184206
58.7k27184206
add a comment |Â
add a comment |Â
up vote
8
down vote
I think that split
is you best approach.
Try using the -l xxxx
option, where xxxx is the number of lines you want in each file (default is 1000).
You can use the -n yy
option if you are more concerned about the amount of files created. Use -n 2
will split your file in only 2 parts, no matter the amount of lines in each file.
You can count the amount of lines in your file with wc -l filename
. This is the 'wordcount' command with the lines option.
References
man split
man wc
1
This is how to split into a bunch of files with a fixed number of lines, or how to split evenly into a fixed number of files. Is there a way to split into one 1000-line file and one file with everything else? That's what he was asking for; I couldn't find it in the man page
â Michael Mrozekâ¦
Oct 21 '14 at 17:05
You´re correct Michael. I think I took a simplistic view on the question. You solution is the best one in this case. Another way would be to use the 'sed' command: sed -n 1,1000 originalfile > first_1000_lines. sed '1,1000d' originalfile > remaining_lines.
â Lucien Raven
Oct 21 '14 at 17:17
Of course you could dosplit -l 1000 bigfile && mv xaa piece1 && cat x?? > piece2 && rm x??
.
â G-Man
Oct 21 '14 at 23:40
add a comment |Â
up vote
8
down vote
I think that split
is you best approach.
Try using the -l xxxx
option, where xxxx is the number of lines you want in each file (default is 1000).
You can use the -n yy
option if you are more concerned about the amount of files created. Use -n 2
will split your file in only 2 parts, no matter the amount of lines in each file.
You can count the amount of lines in your file with wc -l filename
. This is the 'wordcount' command with the lines option.
References
man split
man wc
1
This is how to split into a bunch of files with a fixed number of lines, or how to split evenly into a fixed number of files. Is there a way to split into one 1000-line file and one file with everything else? That's what he was asking for; I couldn't find it in the man page
â Michael Mrozekâ¦
Oct 21 '14 at 17:05
You´re correct Michael. I think I took a simplistic view on the question. You solution is the best one in this case. Another way would be to use the 'sed' command: sed -n 1,1000 originalfile > first_1000_lines. sed '1,1000d' originalfile > remaining_lines.
â Lucien Raven
Oct 21 '14 at 17:17
Of course you could dosplit -l 1000 bigfile && mv xaa piece1 && cat x?? > piece2 && rm x??
.
â G-Man
Oct 21 '14 at 23:40
add a comment |Â
up vote
8
down vote
up vote
8
down vote
I think that split
is you best approach.
Try using the -l xxxx
option, where xxxx is the number of lines you want in each file (default is 1000).
You can use the -n yy
option if you are more concerned about the amount of files created. Use -n 2
will split your file in only 2 parts, no matter the amount of lines in each file.
You can count the amount of lines in your file with wc -l filename
. This is the 'wordcount' command with the lines option.
References
man split
man wc
I think that split
is you best approach.
Try using the -l xxxx
option, where xxxx is the number of lines you want in each file (default is 1000).
You can use the -n yy
option if you are more concerned about the amount of files created. Use -n 2
will split your file in only 2 parts, no matter the amount of lines in each file.
You can count the amount of lines in your file with wc -l filename
. This is the 'wordcount' command with the lines option.
References
man split
man wc
edited Oct 21 '14 at 16:56
slmâ¦
239k65494664
239k65494664
answered Oct 21 '14 at 16:44
Lucien Raven
812
812
1
This is how to split into a bunch of files with a fixed number of lines, or how to split evenly into a fixed number of files. Is there a way to split into one 1000-line file and one file with everything else? That's what he was asking for; I couldn't find it in the man page
â Michael Mrozekâ¦
Oct 21 '14 at 17:05
You´re correct Michael. I think I took a simplistic view on the question. You solution is the best one in this case. Another way would be to use the 'sed' command: sed -n 1,1000 originalfile > first_1000_lines. sed '1,1000d' originalfile > remaining_lines.
â Lucien Raven
Oct 21 '14 at 17:17
Of course you could dosplit -l 1000 bigfile && mv xaa piece1 && cat x?? > piece2 && rm x??
.
â G-Man
Oct 21 '14 at 23:40
add a comment |Â
1
This is how to split into a bunch of files with a fixed number of lines, or how to split evenly into a fixed number of files. Is there a way to split into one 1000-line file and one file with everything else? That's what he was asking for; I couldn't find it in the man page
â Michael Mrozekâ¦
Oct 21 '14 at 17:05
You´re correct Michael. I think I took a simplistic view on the question. You solution is the best one in this case. Another way would be to use the 'sed' command: sed -n 1,1000 originalfile > first_1000_lines. sed '1,1000d' originalfile > remaining_lines.
â Lucien Raven
Oct 21 '14 at 17:17
Of course you could dosplit -l 1000 bigfile && mv xaa piece1 && cat x?? > piece2 && rm x??
.
â G-Man
Oct 21 '14 at 23:40
1
1
This is how to split into a bunch of files with a fixed number of lines, or how to split evenly into a fixed number of files. Is there a way to split into one 1000-line file and one file with everything else? That's what he was asking for; I couldn't find it in the man page
â Michael Mrozekâ¦
Oct 21 '14 at 17:05
This is how to split into a bunch of files with a fixed number of lines, or how to split evenly into a fixed number of files. Is there a way to split into one 1000-line file and one file with everything else? That's what he was asking for; I couldn't find it in the man page
â Michael Mrozekâ¦
Oct 21 '14 at 17:05
You´re correct Michael. I think I took a simplistic view on the question. You solution is the best one in this case. Another way would be to use the 'sed' command: sed -n 1,1000 originalfile > first_1000_lines. sed '1,1000d' originalfile > remaining_lines.
â Lucien Raven
Oct 21 '14 at 17:17
You´re correct Michael. I think I took a simplistic view on the question. You solution is the best one in this case. Another way would be to use the 'sed' command: sed -n 1,1000 originalfile > first_1000_lines. sed '1,1000d' originalfile > remaining_lines.
â Lucien Raven
Oct 21 '14 at 17:17
Of course you could do
split -l 1000 bigfile && mv xaa piece1 && cat x?? > piece2 && rm x??
.â G-Man
Oct 21 '14 at 23:40
Of course you could do
split -l 1000 bigfile && mv xaa piece1 && cat x?? > piece2 && rm x??
.â G-Man
Oct 21 '14 at 23:40
add a comment |Â
up vote
6
down vote
This is a job for csplit
:
csplit -s infile 1001
will s
ilently split infile
, the first piece xx00
- up to but not including line 1001 and the second piece xx01
- the remaining lines.
You can play with the options if you need different output file names e.g. using -f
and specifying a prefix:
csplit -sf piece. infile 1001
produces two files named piece.00
and piece.01
With a smart head
you could also do something like:
head -n 1000 > 1st.out; cat > 2nd.out; < infile
Wow, it really is a job forcsplit
. Very nice. (I'm just reading through the list of POSIX commands and had enormous trouble wrapping my head around thecsplit
command's purpose at first. Turns out it's really really simple.) :)
â Wildcard
Nov 2 '16 at 5:38
add a comment |Â
up vote
6
down vote
This is a job for csplit
:
csplit -s infile 1001
will s
ilently split infile
, the first piece xx00
- up to but not including line 1001 and the second piece xx01
- the remaining lines.
You can play with the options if you need different output file names e.g. using -f
and specifying a prefix:
csplit -sf piece. infile 1001
produces two files named piece.00
and piece.01
With a smart head
you could also do something like:
head -n 1000 > 1st.out; cat > 2nd.out; < infile
Wow, it really is a job forcsplit
. Very nice. (I'm just reading through the list of POSIX commands and had enormous trouble wrapping my head around thecsplit
command's purpose at first. Turns out it's really really simple.) :)
â Wildcard
Nov 2 '16 at 5:38
add a comment |Â
up vote
6
down vote
up vote
6
down vote
This is a job for csplit
:
csplit -s infile 1001
will s
ilently split infile
, the first piece xx00
- up to but not including line 1001 and the second piece xx01
- the remaining lines.
You can play with the options if you need different output file names e.g. using -f
and specifying a prefix:
csplit -sf piece. infile 1001
produces two files named piece.00
and piece.01
With a smart head
you could also do something like:
head -n 1000 > 1st.out; cat > 2nd.out; < infile
This is a job for csplit
:
csplit -s infile 1001
will s
ilently split infile
, the first piece xx00
- up to but not including line 1001 and the second piece xx01
- the remaining lines.
You can play with the options if you need different output file names e.g. using -f
and specifying a prefix:
csplit -sf piece. infile 1001
produces two files named piece.00
and piece.01
With a smart head
you could also do something like:
head -n 1000 > 1st.out; cat > 2nd.out; < infile
edited Aug 26 at 19:37
answered May 10 '15 at 22:54
don_crissti
47.3k15125155
47.3k15125155
Wow, it really is a job forcsplit
. Very nice. (I'm just reading through the list of POSIX commands and had enormous trouble wrapping my head around thecsplit
command's purpose at first. Turns out it's really really simple.) :)
â Wildcard
Nov 2 '16 at 5:38
add a comment |Â
Wow, it really is a job forcsplit
. Very nice. (I'm just reading through the list of POSIX commands and had enormous trouble wrapping my head around thecsplit
command's purpose at first. Turns out it's really really simple.) :)
â Wildcard
Nov 2 '16 at 5:38
Wow, it really is a job for
csplit
. Very nice. (I'm just reading through the list of POSIX commands and had enormous trouble wrapping my head around the csplit
command's purpose at first. Turns out it's really really simple.) :)â Wildcard
Nov 2 '16 at 5:38
Wow, it really is a job for
csplit
. Very nice. (I'm just reading through the list of POSIX commands and had enormous trouble wrapping my head around the csplit
command's purpose at first. Turns out it's really really simple.) :)â Wildcard
Nov 2 '16 at 5:38
add a comment |Â
up vote
4
down vote
A simple way to do what the question asks for, in one command:
awk ' if (NR <= 1000) print > "piece1"; else print > "piece2"; ' bigfile
or, for those of you who really hate to type long, intuitively comprehensible commands,
awk ' print > ((NR <= 1000) ? "piece1" : "piece2"); ' bigfile
add a comment |Â
up vote
4
down vote
A simple way to do what the question asks for, in one command:
awk ' if (NR <= 1000) print > "piece1"; else print > "piece2"; ' bigfile
or, for those of you who really hate to type long, intuitively comprehensible commands,
awk ' print > ((NR <= 1000) ? "piece1" : "piece2"); ' bigfile
add a comment |Â
up vote
4
down vote
up vote
4
down vote
A simple way to do what the question asks for, in one command:
awk ' if (NR <= 1000) print > "piece1"; else print > "piece2"; ' bigfile
or, for those of you who really hate to type long, intuitively comprehensible commands,
awk ' print > ((NR <= 1000) ? "piece1" : "piece2"); ' bigfile
A simple way to do what the question asks for, in one command:
awk ' if (NR <= 1000) print > "piece1"; else print > "piece2"; ' bigfile
or, for those of you who really hate to type long, intuitively comprehensible commands,
awk ' print > ((NR <= 1000) ? "piece1" : "piece2"); ' bigfile
edited Oct 21 '14 at 21:59
answered Oct 21 '14 at 21:11
G-Man
11.8k92658
11.8k92658
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f163399%2fsplit-a-file-into-two%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Did you check
split --help
?â Braiam
Oct 21 '14 at 16:01
Yes i have checked it, but is creating multiple files which doesn't need to me.
â Aravind
Oct 21 '14 at 16:02