Splitting av file in 2 at specific place based on textpattern


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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 10:59 AM.. Reason: Better examples
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #938
Difficulty: Medium
The ping utility was written by Mike Muuss in December 1983.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

How to merge variable data from another file into specific place?

Hello, I'm trying to create multiple commands using a variable input from another file but am not getting any successful results. Basically, file1.txt contains multiple lines with single words: <file1.txt> yellow blue black white I want to create multiple echo commands with these... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the... (9 Replies)
Discussion started by: barrydocks
9 Replies

4. Shell Programming and Scripting

Splitting a file based on context.

I have file as shown below. Would like to split the file based on the context of data. Like, split the content between "---- XXX Info ----" and " ---- YYY Info ----" to a file. When I try using below command, 2nd file contains all the info starting after first "---- YYYY Info ----" instance.... (8 Replies)
Discussion started by: webkid
8 Replies

5. Shell Programming and Scripting

Read from file specific place in file using inode

Hello, I am using tcsh on AIX. I would like to write a script that does the following: 1. given an inode, how do I find exactly the name of the file? I know I could do this using ls -i | grep <inode> but it returns: <inode> <filename>. I need some string manipulation or something to... (1 Reply)
Discussion started by: lastZenMaster
1 Replies

6. Shell Programming and Scripting

Splitting a file based on two patterns

Hi there, I've an input file as follows: *START 1001 a1 1002 a2 1003 a3 1004 a4 *END *START 1001 b1 1002 b2 1004 b4 *END *START 1001 c1 1004 c4 *END (6 Replies)
Discussion started by: kbirde
6 Replies

7. Shell Programming and Scripting

Splitting the file based on logic

Hello I have a requirement where i need to split the Input fixed width file which contains multiple invoices into multiple files with 2 invoices per file. Each invoice can be identified by its first line's second character which is "H" and sixth character is " " space and the invoice would... (10 Replies)
Discussion started by: dsdev_123
10 Replies

8. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 Replies

9. UNIX for Advanced & Expert Users

Split file at location of textpattern

I have a file that I want to split in 2 (with Bourne shell sh) preferably. The file is a configuration file for several elements and hence consists of a repeated configuration pattern like this: config.txt: #fruit banana #color yellow #surface smooth size 20cm #fruit apple #color green... (6 Replies)
Discussion started by: borgeh
6 Replies

10. Shell Programming and Scripting

insert file 1 at a specific place of file 2

Hello, I need to search in file2 for class A : public B { and insert right after that the content of file1. I am a bit lost as to which tools (which bash functions, awk...). I should use. Thanks for some directions here. Regards (1 Reply)
Discussion started by: JCR
1 Replies
FWIP(4) 						   BSD Kernel Interfaces Manual 						   FWIP(4)

NAME
fwip -- IP over FireWire driver SYNOPSIS
To compile this driver into the kernel, place the following lines in your kernel configuration file: device firewire device fwip Alternatively, to load the driver as a module at boot time, place the following lines in loader.conf(5): firewire_load="YES" if_fwip_load="YES" DESCRIPTION
The fwip driver provides standard IP over FireWire (IEEE 1394) based on the protocols described in RFC 2734 and RFC 3146. The firewire(4) and fwohci(4) drivers must be configured in the kernel as well. This driver supports polling(4) as well if it is compiled with the DEVICE_POLLING option. SEE ALSO
arp(4), firewire(4), fwe(4), fwohci(4), netintro(4), polling(4), ifconfig(8), kldload(8), sysctl(8) HISTORY
The fwip device driver first appeared in FreeBSD 5.3. AUTHORS
The fwip driver and this manual page were written by Doug Rabson, based on earlier work by Hidetoshi Shimokawa. BUGS
This driver currently does not support the MCAP protocol for multicast IP over FireWire. Multicast packets are treated as broadcast packets which is sufficient for most trivial uses of multicast. BSD
July 16, 2005 BSD

Featured Tech Videos