The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting a file based on the records in another file er_ashu Shell Programming and Scripting 2 05-12-2008 01:34 PM
Number Grouped Rows in File RacerX UNIX for Dummies Questions & Answers 2 03-03-2008 11:36 AM
Splitting av file in 2 at specific place based on textpattern borgeh Shell Programming and Scripting 0 09-24-2007 04:02 PM
splitting files based on text in the file matrix1067 Shell Programming and Scripting 1 01-30-2006 04:45 PM
Splitting a file based on some condition and naming them srivsn Shell Programming and Scripting 1 12-07-2005 07:27 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-03-2008
Registered User
 

Join Date: Jun 2008
Posts: 4
Stumble this Post!
Splitting file based on number of rows

Hi,

I'm, new to shell scripting, I have a requirement where I have to split an incoming file into separate files each containing a maximum of 3 million rows.
For e.g: if my incoming file say In.txt has 8 mn rows then I need to create 3 files, in which two will 3 mn rows and one will contain 2 mn rows. The number of rows in In.txt will vary.

My Algorithm is as follows:
I would first do a wc -l on In.txt to get no: of rows
Divide the count/3mn to get no: of files to be created (8/3=2.66 ~3)
Then in a loop use head and tail commands to transfer rows into files into new file

The above algorithm will work, but I think using awk there may be something easier.

Please let me know if there is a faster and easier way of doing this

Regards
Wahi
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-03-2008
Registered User
 

Join Date: Jun 2008
Posts: 4
Stumble this Post!
Smile

I used the split command which reduced the code to one line
Reply With Quote
  #3 (permalink)  
Old 06-03-2008
Registered User
 

Join Date: Jan 2008
Posts: 12
Stumble this Post!
After getting number of rows in one file, you can use sed to split it
Code:
sed '$begin_from,$last_to w new_file1' original_file
as begin_from and last_to are your start and end lines, "w" means write these lines to the following file (which is new_file1 here)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0