Split File into seperate files

Hi,

So I have a text file which I want to separate into separate text files. I would use the split command but the problem here is that the text file is separated by delimiters. For example:

blah
blah blah
------
more text
-----
and some more text

So basically the first part should be one text file and then the middle part (more text) should be its own text file and finally the bottom another one. I was thinking of writing a while loop to do this but I'm a newb to shell coding so maybe someone could help me fill in the blanks:

# Set line to beginning of file
$line = #not sure how to do this

# Shell to save everything above first delimiter in header
while ($line != "-----")
do
        cat file $line >> temp_file
        mv temp_file file
       # increment $line to next line
done

And repeat for the other 2 parts. The original file should really only have 3 parts, maybe 4, so I could just repeat the loop but if someone wants to make this efficient be my guest. Any help would be much appreciated. Thanks so much!

Elt

I'd use csplit for this. The following script demonstrates how you can apply it

$ cat ./split_it.sh 
#!/bin/bash

rm -f split_file part.??

cat >split_file <<EOF
a
b
c
-----
d
e
f
-----
g
h
i
-----
j
k
l
EOF

csplit -k -s -f part. split_file /^-----/ "{100}" 2>/dev/null

ls part.?? | while read file; do
   sed '/^-----/ d' ${file} > ${file}.new && mv ${file}.new ${file}
   echo "File ${file} contains:"
   cat ${file}
done

exit 0
$ ./split_it.sh 
File part.00 contains:
a
b
c
File part.01 contains:
d
e
f
File part.02 contains:
g
h
i
File part.03 contains:
j
k
l

Cheers,
ZB

Cool! This works really nicely, thanks so much =D

Sorry, one more quick question. If I wanted to keep the delimiter in the separate files, how would that change the code. For example if my example is:

header
-----
body
-----
footer

The separate files should have the text and then delimiter following it with the exception of the last file. So the first file would be:

header
------

Thanks!

To keep the delimiter in each file, just remove the 'sed':

sed '/^-----/ d' ${file} > ${file}.new && mv ${file}.new ${file}

And the code will be:

csplit -k -s -f part. split_file /^-----/ "{100}" 2>/dev/null
ls part.?? | while read file; do
   echo "File ${file} contains:"
   cat ${file}
done