LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-18-2009, 08:32 AM   #1
imkornhulio
Member
 
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
adnanm
Member
 
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
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Split file in halve. The created files with the halves will be called "file.part00" and "file.part01" in the curren directory:
Code:
FILE=/tmp/file.csv
split -d -l $(($(wc -l $FILE | cut -d' ' -f1)/2)) $FILE file.part
 
Old 03-18-2009, 09:28 AM   #4
imkornhulio
Member
 
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
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
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:
Code:
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.
 
  


Reply



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

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

All times are GMT -5. The time now is 03:25 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