bash: how to split a text file in two unequal parts
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash: how to split a text file in two unequal parts
Hi,
While googling around to figure this out, I came across this thread, and thought this seemed like a friendly forum, so here's a follow-up:
I am splitting a series of text files in 10% and 90% sized parts by line count (for testing and training of a machine learning program).
The best way I have found to do this, is to use split to create ten 10% segments, and then use cat to splice nine of them together.
It works, but it seems unnecessarily roundabout. Anyone got a better idea?
BTW: I tried using head and tail instead. head worked fine, it prints the first n lines of the file as specified, but I can't get tail to print the last n lines of the file.
Ain't no way to do it without reading the file - maybe more than once.
Given the way (sequential) disk reads are cached, it'd be almost better to re-read the file, and split on a re-run, after all the maths is done.
Saves trashing storage by trying to do everything in memory - especially if you can't (accurately) predict the filesize. For "one-offs", sed is as easy as anything, else I'd use perl - I'm sure those that can spell awk could do it just as well.
Last edited by syg00; 11-22-2007 at 05:57 AM.
Reason: 'cos I didn't read the inital post properly ...
#!/usr/bin/perl
open TEN, "> 10_percent.lis" or die "Ooops:$!";
open NINETY, "> 90_percent.lis" or die "Ooops:$!";
while (<>) {
if ($count++ % 10) {
print TEN;
} else {
print NINETY;
}
}
Code:
billy %wc -l 1
100 1
billy %1.pl 1
billy %wc -l *percent.lis
90 10_percent.lis
10 90_percent.lis
100 total
edit Oops!
wrong way round, but you get the idea
Last edited by bigearsbilly; 11-22-2007 at 07:03 AM.
If you had really long files, and if you would settle for approximations, you could estimate the number of lines in a file. The stat function is very cheap. Once you have one datum, namely the size of the file in bytes, you calculate the number of lines based on, say, 60 characters per line average.
Here is an output from such a perl script running on two files, one the first chapter of Moby Dick, second a 1 MB file of an artificial mixture of dictionary words and nonsense-created words. The script writes two files, one "ten", the other "ninety".
This allows one to make a single pass over the file ... cheers, makyo
Code:
% ./s1
( Lines read: 206 )
length of /home/makyo/not-backed-up/data/moby11.txt is 12440 bytes.
number of lines in /home/makyo/not-backed-up/data/moby11.txt estimated 207
Comparison to wc:
File /home/makyo/not-backed-up/data/moby11.txt has 206 lines.
File ten has 20 lines.
Percent = 9.700
( Lines read: 16569 )
length of /home/makyo/not-backed-up/data/one-mb is 1013995 bytes.
number of lines in /home/makyo/not-backed-up/data/one-mb estimated 16899
Comparison to wc:
File /home/makyo/not-backed-up/data/one-mb has 16569 lines.
File ten has 1689 lines.
Percent = 10.100
Thanks a bunch for your answers. I feel kinda stupid now, since *tail* is suddenly working like it should. Can't really say what I did wrong the last time...
So I guess that will do the trick. I'll have a look at the more advanced solutions you've posted too, though. Some of them are bound to be more optimal, I suppose. And in any case instructive
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.