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
Need help with awk - how to read a content of a file from every file from file list tanit Shell Programming and Scripting 7 03-10-2009 05:19 AM
file size comparision local file and remote file dba.admin2008 Shell Programming and Scripting 4 11-13-2008 05:57 PM
PDF Splitter & Merger: 3.0 released iBot Software Releases - RSS News 0 11-04-2008 06:00 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 05:25 AM
file splitter ECBROWN UNIX for Dummies Questions & Answers 0 09-28-2005 03:17 PM

Reply
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 1 Week Ago
sitney sitney is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 19
File splitter by nth row

I need to split a file into n separate files of about the same size. The way the file will be split is at every nth row, starting with the first row, that row will be cut and copied to it's corresponding new file so that each file has unique records. Any 'leftovers' will go into the last file. e.g.
Code:
>ls 
sample.txt
> cat sample.txt
aab
aac
aad
aae
aaf
aag
aah
aai
aaj
aak
aal
aam
'Run command' with n=4
Desired output:
Code:
>ls -l
sample.txt
sample1.txt
sample2.txt
sample3.txt
sample4.txt
>cat sample1.txt
aab
aae
aai
aam
>cat sample2.txt
aac
aag
aal
>cat sample3.txt
aad
aaj
>cat sample4.txt 
aaf
aah
aak
# sample4.txt picked up the "leftovers".

Thanks in advance.
  #2 (permalink)  
Old 1 Week Ago
dddkiran dddkiran is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 24
split -l 4 sample.txt <prefix>
  #3 (permalink)  
Old 1 Week Ago
sitney sitney is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 19
The problem with the split command is that it takes sequential rows from the target file. It does not pull every nth line from the file as requested. This won't work.
  #4 (permalink)  
Old 1 Week Ago
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
Your required output is not consistent.
Try this solution:
Code:
awk '{print > ("sample"++c".txt");c=(NR%n)?c:0}' n=4 file
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
  #5 (permalink)  
Old 1 Week Ago
sitney sitney is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 19
Thanks Danmero. This works perfectly. If you could unpack this part of the Awk statement for my learning benfit, that would be extremely helpful and appreciated.

Code:
c=(NR%n)?c:0}
Sponsored Links
Reply

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 07:44 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