Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11th December 2007, 04:15 AM
gold Offline
Registered User
 
Join Date: Aug 2007
Posts: 78
noob: Need Break up large txt file

I have a large text file. I want to break the text file in many equal parts based on number of total lines in the txt file. eg. txt file has 1500 lines, then 3 equal small files would have 500 lines in each.

What is the best way to break large txt file into parts base on # of lines?
Reply With Quote
  #2  
Old 11th December 2007, 04:41 AM
marko's Avatar
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449
If you have a file "bigfile" then you can use split to cut it up into
500 line chunks:

split -500 bigfile

unfortunately split names the output files in kind of a dumb way:

xaa, xab, xab .......

that is the output files have no naming relation to the input file,
I would have thought bigfile.01, bigfile.02 .... etc would be
more sensible.

"wc" is the handy way to find out how many lines the input
file has to decide how big to make the chunk files:

> wc -l bigfile
14416 bigfile

so if you want three files of about equal size split -d -5000 bigfile
would be a good try with my 14416 line example.
(the -d option sets up the suffix to be two digits which you might like better than the aa, ab, ac, ... default)

see the man page for "split" for more

Mark

Last edited by marko; 11th December 2007 at 04:47 AM.
Reply With Quote
  #3  
Old 11th December 2007, 11:07 AM
HaydnH's Avatar
HaydnH Offline
Registered User
 
Join Date: Feb 2005
Location: London, UK
Posts: 333
If you put the filename twice after split it will relate to the filename, you can also split the file by number of bytes, e.g:

Code:
[root@localhost test]# dd if=/dev/zero of=./bigfile bs=1024k count=5
5+0 records in
5+0 records out
5242880 bytes (5.2 MB) copied, 0.0595227 s, 88.1 MB/s
[root@localhost test]# split --bytes=1024k bigfile bigfile.
[root@localhost test]# ls -lh
total 11M
-rw-r--r-- 1 root  root  5.0M 2007-12-11 11:00 bigfile
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.aa
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ab
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ac
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ad
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ae
[root@localhost test]#
And you can rebuild the big text file with the following:

Code:
[root@localhost test]# rm -f bigfile
[root@localhost test]# for i in bigfile.*; do cat "$i" >> ./bigfile; done
[root@localhost test]# ls -lh
total 11M
-rw-r--r-- 1 root  root  5.0M 2007-12-11 11:07 bigfile
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.aa
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ab
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ac
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ad
-rw-r--r-- 1 root  root  1.0M 2007-12-11 11:01 bigfile.ae
[root@localhost test]#
Reply With Quote
Reply

Tags
break, file, large, noob, txt

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
unzipping a large file crossmr Using Fedora 31 7th March 2011 05:24 PM
'File too large' when copying 6.3GB file from ext3 to USB arun_maurya Using Fedora 4 8th December 2008 11:24 AM
Problem: file too large Reisswolf Using Fedora 1 11th July 2007 07:49 AM
break file into multiple parts capo Using Fedora 1 22nd May 2006 03:30 AM
tar -M to break large file into chunks? tashirosgt Using Fedora 2 22nd July 2005 04:41 PM


Current GMT-time: 02:44 (Saturday, 25-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat