Parsing a file

Hi,
Please help in parsing the following file and write separate files by parsing the file for the new file's content.

Main File:
------------

BEGIN
FileName: FirstFile.txt
Content of the File Start
AAAAAAAA
BBBBBBB
Content of the File End
END
BEGIN
FileName: SecondFile.txt
Content of the File Start
CCCCCC
DDDDDD
Content of the File End
END
BEGIN
FileName: ThirdFile.txt
Content of the File Start
EEEEEEE
FFFFFFFF
Content of the File End
END
BEGIN
FileName: FourthFile.txt
Content of the File Start
YYYYYYYY
ZZZZZZZZ
Content of the File End
END

OUTPUT:
Four files with the file names FirstFile.txt,SecondFile.txt,ThirdFile.txt and FourthFile.txt
and content of the files will be between BEGIN and END except FileName
eg:
FirstFile.txt:
##################
Content of the File Start
AAAAAAAA
BBBBBBB
Content of the File End
##################

Thanks in Advance

awk '/END/ {flag = 0}
     flag  {print > of}
     /FileName/ {flag = 1;of = $2}' file

Hope this Should Work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#!/usr/bin/ksh

i=1
j=1
cat samp | while read line
do
val="END"
value="BEGIN"
if [ $j -le 7 ]
then
if [[ $line != $val && $line != $value ]]
then
echo $line
echo $line >> File$i.txt
j=`expr $j + 1`
fi
if [ $j -eq 7 ]
then
j=0
i=`expr $i + 1`
fi
fi
done

Regards,
Anand

Thanks for Shamrock & Anand for the quick response and sorry for the late acknowledgement

Hi Shamrock & Anand,
I am trying to parse the similar (below mentioned sample.txt) file using the following script. But I am having the following issue:

  1. Whitespaces of the file are not maintained.
  2. Text (****************************************) is not getting printed.Its getting translated to the script file name.

Script:
#!/usr/bin/sh
i=1
cat sample.txt | while read line
do
var1="END"
var2="BEGIN"
toPrint=`echo $line | cut -c1-7`
toPrintText=`echo $line | cut -c8-`
elif [[ $toPrint == 'TOPRINT' ]]
then
echo toPrintText
elif [[ $line != $var1 && $line != $var2 ]]
then
echo $line >> File$i.txt
elif [[ $line == $var1 ]]
then
i=`expr $i + 1`
fi
done

=========
sample.txt

BEGIN
First File Content Starts here
****************************************

	Something or the other here

TOPRINT=This is the text to print on the consolee....
This is the end of the File content
END
BEGIN
Second File Content Starts here
****************************************

	Something or the other here

TOPRINT=This is the text to print on the consolee....
This is the end of the File content
END
BEGIN
Third File Content Starts here
****************************************

	Something or the other here

TOPRINT=This is the text to print on the consolee....
This is the end of the File content
END

Appreciate your response!

Thanks

Both of these are symptoms of inadequate quoting. The asterisk is a wildcard which gets expanded to a list of file names unless you quote it; perhaps it would be wise to use a different separator.

(This, of course, is a Useless Use of Cat. The recommended idiom is

while read line
do
   : ...
done <sample.txt

... But don't worry too much about this stylistic detail.)

See how far you can get just by judiciously quoting everything. The use of echo inside backticks is probably going to ruin some of your whitespace anyway -- quite frankly, I would not use a shell script for this. But try modifications like

var1="END"
var2="BEGIN"
toPrint="`echo "$line" | cut -c1-7`" 
toPrintText="`echo "$line" | cut -c8-`" 
elif [[ "$toPrint" == 'TOPRINT' ]]
	then
	echo "$toPrintText"

(I also added the missing dollar sign on the last line.)

Your "elif" seems to be missing the initial "if" but that's trivial, of course.

awk 'BEGIN{n=1;FS=":"}
$0 ~ /^BEGIN/ {flag=1;getline;findex=$2;getline}
$0 ~ /^END/ {flag=0;n=n+1}
{
if (flag==1)
file[findex]=sprintf("%s\n%s",file[findex],$0)
}
END{
for (i in file)
{
print i" ----> "file
print "-----------------"
}
}' filename

Thanks a lot era & summer cherry for the quick responses!!!

I am able to process the file fine. I am using the following script:
awk 'BEGIN{n=1;FS=":";f=0}
$0 ~ /^START:/ {flag=1;getline;findex=$2}
$0 ~ /^END:/ {flag=0;n=n+1}
$0 ~ /^ EMAIL:/ {flag=2}

{
if (flag==1)
print $0 > "File"n

if (flag==2){
flag=1
email = $2}

if (flag==0){
f=n-1
mailx -s "SUB" $email < File$f
}

}

END{

}' filename

Please let me know how to mail from within the awk script. I am getting error at line
mailx -s "SUB" $email < File$f

Thanks.

cat File$f | mailx -s "SUB" $email 

I don't see where $email is set, either

Thanks for the reply jim!

I am still getting the error.

I am reading the email from the file.But even when hardcode the email still I am getting the following error:
awk: syntax error near line 16
awk: illegal statement near line 16

Also if I do the cat will remove the spaces?

Thanks

mailx is not a valid awk command, you can't use it from within an awk script (other than with a contortion such as system() or exec()). I would set this up so that the awk script produces a report and a shell script runs the awk script and pipes it to mailx.

cat does not modify its output in any way, but is also useless for this. It's mainly just a stylistic issue, but anything involving cat singlefile | othercommand is better phrased like othercommand < singlefile

Thanks everyone for the help!