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 file based on number of rows wahi80 Shell Programming and Scripting 2 06-03-2008 10:38 PM
Splitting a file based on record sin another file er_ashu UNIX for Dummies Questions & Answers 7 05-16-2008 12:06 AM
insert file 1 at a specific place of file 2 JCR Shell Programming and Scripting 1 02-01-2007 11:48 AM
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

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-24-2007
borgeh borgeh is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 12
Splitting file in 2 at specific place based on textpattern

I have a file that I want to split in 2 (with Bourne shell sh) preferably. The file consists of groups of lines separated by newline. The file can vary in length, so I need to check number of groups of text. Here's an example

====EXAMPLE START====
#fruit banana
#color yellow
#surface smooth
size 20cm

#fruit apple
#color green
#surface smooth
size 7cm

#fruit grape
#color green
#surface smooth
size 2cm
====EXAMPLE STOP====

I want to split the file in 2 equal pieces in the sense that there will be equal amount of "fruits" (or groups of texts) in each. But the file should only be split just before a "#fruit" tag so that no fruit-text is split in the middle.
By equal pieces I mean that if the file has data for 4 fruits it should split it so that I will have 2 files with 2 fruits in both. 3 fruits should split to 2+1. 7 fruits should split to 4+3 a.s.f. The example above would result in 2 files:
======
File1:
#fruit banana
#color yellow
#surface smooth
size 20cm

#fruit apple
#color green
#surface smooth
size 7cm
======
File2:
#fruit grape
#color green
#surface smooth
size 2cm
======

I can easily find number of fruits by doing:
i=`grep "# fruit" fruitfile | wc -l`

...and then find after which group I should split:

j=`expr $i / 2`

But from here I am more unsure.

Borge

Last edited by borgeh; 09-26-2007 at 09:59 AM.. Reason: Better examples
 

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 12:16 AM.


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