How to split file based on subtitle

Hi, unix Gurus,
I want to split file based on sub_title. for example:

original file
fruit
apple
watermelon
meat
pork
fish
beef

expected result file

file1
fruit
apple
watermelon
file2
meat
pork
fish

beef.
:wall:

Thanks in advance

This question is like:
"I want to split binary string based on binary number. For example 011000101010110101. I want 01100010 and 1010110101"

I think the point here is not unix-skills.
In your "original file", the content must be "well sorted". e.g.

fruit
fruit1
fruit2
...
fruitn
meat
meat1
meat2
...
meatn
vegetable
vegetable1
vegetable2
...
vegetablen
...

you need to give a keyword/category list, it could be a file, that defines the different categories by name("fruit", "meat", "vegetable")

then awk can split your original file as your requirement easily.

i m not clearly but i try something related split..

# ./justdoit2 file 3
Showing 'numbered 'file1' text file ..
fruit
apple
watermelon
 
Showing 'numbered 'file2' text file ..
meat
pork
fish
 
Showing remaining lines that 'not saved a file' ..
beef
beef2
 
## justdoit split ## 
#!/bin/bash
i=0;f=0;l=$2;file=$1
c=$(echo $(sed -n '$=' $1) /3|bc)
while [ $(( c -=1 )) -gt -1 ] ; do
((i++));sed -n "$(($f+1)),$(($f+$l)) p" $file >$file${i};f=$(($f++$l));
done
while [ $(( x +=1 )) -le $i ] ; do
echo -e "Showing 'numbered '$file${x}' text file .. \n$(more $file${x})\n"
done
echo -e "Showing remaining lines that 'not saved a file' .. \n$(sed -n "$(($f+1)),$ p" $file)\n"

or try like this if you has split (files starts index 0.i.e..file0 , file2..)

# split -l3 -d -a1 file file

regards
ygemici

Hi.

There is a perl code that performs like csplit (q.v.), but with alternation allowed (because it's a perl code). So to split a file into sections, one could do something like this, (assuming that the code is downloaded to a local file ppt-split). Here is the driver code, see the URL if you are interested in this or other perl work-alikes:

#!/usr/bin/env bash

# @(#) s1	Demonstrate perl version of split with regular expressions.

split=./ppt-split
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C $split
v1="cpansearch.perl.org/src/CWEST/ppt-0.14/html/commands/split/split.lafferty"
pe "http://${v1}"

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

rm -f x*
pl " Results, single heading, number:"
$split -p 'string' $FILE
ls -lgG x*

rm -f x*
pl " Results, multi-heading, number|string:"
$split -p 'number|string' $FILE
ls -lgG x*

pl " Sample of one of the files, x.aac:"
cat x.aac

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
ppt-split - ( local: RepRev 1.2, ./ppt-split, 2011-06-18 )
http://cpansearch.perl.org/src/CWEST/ppt-0.14/html/commands/split/split.lafferty

-----
 Input data file data1:
string
able
baker
charlie
number
1
2
3
string
dog
easy
fox
number
4
5
6

-----
 Results, single heading, number:
-rw-r--r-- 1  0 Jun 18 15:14 x.aaa
-rw-r--r-- 1 39 Jun 18 15:14 x.aab
-rw-r--r-- 1 33 Jun 18 15:14 x.aac

-----
 Results, multi-heading, number|string:
-rw-r--r-- 1  0 Jun 18 15:14 x.aaa
-rw-r--r-- 1 26 Jun 18 15:14 x.aab
-rw-r--r-- 1 13 Jun 18 15:14 x.aac
-rw-r--r-- 1 20 Jun 18 15:14 x.aad
-rw-r--r-- 1 13 Jun 18 15:14 x.aae

-----
 Sample of one of the files, x.aac:
number
1
2
3

This kind of thing -- acquiring and downloading utilities that are usually more general than single-line solutions -- is not for everyone, but it is a useful technique that can be used to add to one's personal / private / professional toolset ... cheers, drl