From: Rocky Jr. on
My data file
----
zone "data1.txt" {
type gb2;
file "/nas/data1.txt";
};

zone "delta.doc" {
type gb2;
file "/nas/delta.doc";
};

zone "meta.exe" {
type gb2;
file "/nas/meta.exe";
};
----

I need to select text between zone and }; so I wrote
awk '/^zone/, /^};$/' input.file

This works great and but I need to store selected data in file called
delta.doc or meta.exe, in short select the following and store to
data1.txt:

zone "data1.txt" {
type gb2;
file "/nas/data1.txt";
};

The file has over 100s of entry and I need to create 100 new files
from a single file. How do I ask awk to select data1.txt from 'zone
"data1.txt"' ?
From: Janis Papanagnou on
Rocky Jr. wrote:
> My data file
> ----
> zone "data1.txt" {
> type gb2;
> file "/nas/data1.txt";
> };
>
> zone "delta.doc" {
> type gb2;
> file "/nas/delta.doc";
> };
>
> zone "meta.exe" {
> type gb2;
> file "/nas/meta.exe";
> };
> ----
>
> I need to select text between zone and }; so I wrote
> awk '/^zone/, /^};$/' input.file
>
> This works great and but I need to store selected data in file called
> delta.doc or meta.exe, in short select the following and store to
> data1.txt:
>
> zone "data1.txt" {
> type gb2;
> file "/nas/data1.txt";
> };
>
> The file has over 100s of entry and I need to create 100 new files
> from a single file. How do I ask awk to select data1.txt from 'zone
> "data1.txt"' ?

I suppose it would be as good for your task to not only select a single
block but create the files in one pass? Then try...

awk 'BEGIN { c=1 ; f="f"c }
/^zone/, /^};$/ { print > f }
!NF { close(f) ; f="f"++c }'

which will create files f1..f100 from the respective blocks.

Janis
From: Janis Papanagnou on
Janis Papanagnou wrote:
> Rocky Jr. wrote:
>> My data file
>> ----
>> zone "data1.txt" {
>> type gb2;
>> file "/nas/data1.txt";
>> };
>>
>> zone "delta.doc" {
>> type gb2;
>> file "/nas/delta.doc";
>> };
>>
>> zone "meta.exe" {
>> type gb2;
>> file "/nas/meta.exe";
>> };
>> ----
>>
>> I need to select text between zone and }; so I wrote
>> awk '/^zone/, /^};$/' input.file
>>
>> This works great and but I need to store selected data in file called
>> delta.doc or meta.exe, in short select the following and store to
>> data1.txt:
>>
>> zone "data1.txt" {
>> type gb2;
>> file "/nas/data1.txt";
>> };
>>
>> The file has over 100s of entry and I need to create 100 new files
>> from a single file. How do I ask awk to select data1.txt from 'zone
>> "data1.txt"' ?
>
> I suppose it would be as good for your task to not only select a single
> block but create the files in one pass? Then try...
>
> awk 'BEGIN { c=1 ; f="f"c }
> /^zone/, /^};$/ { print > f }
> !NF { close(f) ; f="f"++c }'
>
> which will create files f1..f100 from the respective blocks.

And another (shorter) solution...

awk 'BEGIN { RS="" } /^zone/, /^};$/ { f="f"++c ; print > f ; close(f) }'

>
> Janis
From: Rocky Jr. on
On May 10, 1:55 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> Janis Papanagnou wrote:
> > Rocky Jr. wrote:
> >> My data file
> >> ----
> >> zone "data1.txt" {
> >>         type gb2;
> >>         file "/nas/data1.txt";
> >> };
>
> >> zone "delta.doc" {
> >>         type gb2;
> >>         file "/nas/delta.doc";
> >> };
>
> >> zone "meta.exe" {
> >>         type gb2;
> >>         file "/nas/meta.exe";
> >> };
> >> ----
>
> >> I need to select text between zone and }; so I wrote
> >> awk '/^zone/, /^};$/' input.file
>
> >> This works great and but I need to store selected data in file called
> >> delta.doc or meta.exe, in short select the following and store to
> >> data1.txt:
>
> >> zone "data1.txt" {
> >>         type gb2;
> >>         file "/nas/data1.txt";
> >> };
>
> >> The file has over 100s of entry and I need to create 100 new files
> >> from a single file. How do I ask awk to select data1.txt from 'zone
> >> "data1.txt"' ?
>
> > I suppose it would be as good for your task to not only select a single
> > block but create the files in one pass?  Then try...
>
> >   awk 'BEGIN { c=1 ; f="f"c }
> >        /^zone/, /^};$/ { print > f }
> >       !NF { close(f) ; f="f"++c }'
>
> > which will create files f1..f100 from the respective blocks.
>
> And another (shorter) solution...
>
>   awk 'BEGIN { RS="" } /^zone/, /^};$/ { f="f"++c ; print > f ; close(f) }'
>
>
>
>
>
> > Janis

Thanks Janis!

It is working as {f1..f100}. Is there any way we can replace f1 with
actual name such as data1.txt ans so on?
From: Janis Papanagnou on
Rocky Jr. wrote:
> On May 10, 1:55 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> wrote:
>> Janis Papanagnou wrote:
>>> Rocky Jr. wrote:
>>>> My data file
>>>> ----
>>>> zone "data1.txt" {
>>>> type gb2;
>>>> file "/nas/data1.txt";
>>>> };
>>>> zone "delta.doc" {
>>>> type gb2;
>>>> file "/nas/delta.doc";
>>>> };
>>>> zone "meta.exe" {
>>>> type gb2;
>>>> file "/nas/meta.exe";
>>>> };
>>>> ----
>>>> I need to select text between zone and }; so I wrote
>>>> awk '/^zone/, /^};$/' input.file
>>>> This works great and but I need to store selected data in file called
>>>> delta.doc or meta.exe, in short select the following and store to
>>>> data1.txt:
>>>> zone "data1.txt" {
>>>> type gb2;
>>>> file "/nas/data1.txt";
>>>> };
>>>> The file has over 100s of entry and I need to create 100 new files
>>>> from a single file. How do I ask awk to select data1.txt from 'zone
>>>> "data1.txt"' ?
>>> I suppose it would be as good for your task to not only select a single
>>> block but create the files in one pass? Then try...
>>> awk 'BEGIN { c=1 ; f="f"c }
>>> /^zone/, /^};$/ { print > f }
>>> !NF { close(f) ; f="f"++c }'
>>> which will create files f1..f100 from the respective blocks.
>> And another (shorter) solution...
>>
>> awk 'BEGIN { RS="" } /^zone/, /^};$/ { f="f"++c ; print > f ; close(f) }'
>>
>>
>>
>>
>>
>>> Janis
>
> Thanks Janis!
>
> It is working as {f1..f100}. Is there any way we can replace f1 with
> actual name such as data1.txt ans so on?

Sure. Replace f = "f" ++c by f = "data" ++c ".txt" in the second proposal.

Janis