Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-18-2009, 08:32 AM   #1
Registered: Aug 2008
Posts: 58

Rep: Reputation: 15
Split file in two

i have a Large file about 10,000 lines and i want to split it in to two files 5000 Lines each how do i go about and do it. (its a csv file)

Thanks for the help.
Old 03-18-2009, 09:06 AM   #2
Registered: Dec 2004
Location: Bosnia and Herzegowina
Distribution: Centos, FreeBSD
Posts: 49

Rep: Reputation: 15
try this
Old 03-18-2009, 09:13 AM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Split file in halve. The created files with the halves will be called "file.part00" and "file.part01" in the curren directory:
split -d -l $(($(wc -l $FILE | cut -d' ' -f1)/2)) $FILE file.part
Old 03-18-2009, 09:28 AM   #4
Registered: Aug 2008
Posts: 58

Original Poster
Rep: Reputation: 15
I used code below (works fine)

split -l 6000 myfile.csv newfile

Thanks for all your help
Old 03-18-2009, 11:21 AM   #5
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Originally Posted by imkornhulio View Post
I used code below (works fine)

split -l 6000 myfile.csv newfile
But are you going to continue to use these as .csv files?

You'll notice that a real .csv file, the first line is a special case. It names the fields for a line.

So after you split your files, do this:
head -5 second_file_name
That will output the first five lines of your second file. Look very closely at the first line.

Then go to some application which reads the second file. Does it see that first line as data, or does it skip over the first line entirely, or what?

If the first line in the second file is not seen by your application, then you may wish to give more consideration to adnanm's solution above, even though it seems more complex. It's more complex because it makes sure that your header line appears as the first line of each new file, rather than having a line of data masquerade as a header line, thus not being seen by your application.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 file wolfipa Programming 3 12-11-2007 05:18 AM
How to split file , .. awk or split ERBRMN Linux - General 9 08-15-2006 12:02 AM
mysqldump : Can I split the file up to 2GB max per file? Swakoo Linux - General 10 10-17-2005 04:13 AM
split one file to two alaios Linux - General 1 05-19-2005 10:24 PM
split file farkmischer Linux - Laptop and Netbook 3 11-28-2004 07:19 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

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