The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 04:34 PM
Number Grouped Rows in File RacerX UNIX for Dummies Questions & Answers 2 03-03-2008 03:36 PM
Splitting av file in 2 at specific place based on textpattern borgeh Shell Programming and Scripting 0 09-24-2007 07:02 PM
splitting files based on text in the file matrix1067 Shell Programming and Scripting 1 01-30-2006 08:45 PM
Splitting a file based on some condition and naming them srivsn Shell Programming and Scripting 1 12-07-2005 11:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-03-2008
wahi80 wahi80 is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 4
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
  #2 (permalink)  
Old 06-03-2008
wahi80 wahi80 is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 4
Smile

I used the split command which reduced the code to one line
  #3 (permalink)  
Old 06-03-2008
xxmenxx xxmenxx is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 12
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)
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0