LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2007, 05:06 AM   #1
Humbro
LQ Newbie
 
Registered: Nov 2007
Posts: 5

Rep: Reputation: 0
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.

Thanks in advance
 
Old 11-22-2007, 05:21 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
????

last n lines...

tail -n
 
Old 11-22-2007, 05:35 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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 ...
 
Old 11-22-2007, 07:02 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
near as dammit?

Code:
#!/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.
 
Old 11-22-2007, 07:18 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
This will read the file twice. First to get the number of lines, and the second time to split the file.
Code:
length=$(wc -l file)
top=$(($length/10))

sed -n "1,${top}w topfile" file  
sed -n "$(($top+1)),\$w bottomfile" file
You could also use
head -n $top file >topfile
tail -n $(($length-$top)) file >bottomfile

Last edited by jschiwal; 11-22-2007 at 07:26 AM.
 
Old 11-22-2007, 11:20 PM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

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
 
Old 11-23-2007, 03:20 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well, if yo get the size in bytes you can use
tail -c
 
Old 11-23-2007, 07:15 AM   #8
Humbro
LQ Newbie
 
Registered: Nov 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
  


Reply

Tags
bash, file



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split data of text file into mysql with perl koscek Programming 1 11-01-2007 10:26 AM
how to sort text file and split into smaller files michaeljoser Linux - Software 8 10-19-2007 01:50 AM
Copying parts of a text file austen77 Linux - Newbie 3 09-01-2005 09:56 PM
bash: split text file iluvatar Programming 4 08-22-2005 08:58 AM
split up text file jollyjoice Programming 4 06-10-2005 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration