Reputation: 35
I want to divide (or split) a big txt file into short txt files, for example I have file.txt
and it's 700 MB, and I want to divide it into seven (100 MB) txt files.
What am I going to do? Is there any way to divide it without opening the text file and change the content of text file? If there are known algorithms for this problem, please share them. Thanks
Upvotes: 0
Views: 763
Reputation: 9026
The Perl distribution includes a split utility, if you happen to not be on Unix. It uses -l for line split.
Upvotes: 0
Reputation: 3454
In case, you want to have both options of splitting by size and by number of line (which in some cases you need) and it is in Windows environment, you may use a program called GSplit. I once had this problem and it worked for me. You can google it to download.
Upvotes: 0
Reputation: 6841
Simple approach -
Get the file length, decide the chunk size
See how many files you want to create - (n = file length/ chunk size). If any remainder, number of files would be n + 1.
Open the file in read mode.
Create a file in write mode. (probably append a suffix 1..n to indicate a fragment)
write chunk number of bytes (or remainder bytes if remainder < chunk size) to the file, close this file.
repeat 4-5 for n fragments.
Upvotes: 2
Reputation: 11862
Just in case it applies to your environment and needs, the standard Unix solution is to use split
:
split -C 100M input.txt
-C
forces split to split by line instead of byte/char. Use -b
instead if you prefer to split at a specific byte.
Upvotes: 7
Reputation: 96177
If you are on unix
split -b100m filename part
will split filename into parta, partb, partc etc
Upvotes: 2