![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
| Display Modes | Rate This Thread |
|
|