From: Laurianne Gardeux on
Rocky Jr. à écrit :

> 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?


awk 'BEGIN { RS="" }/^zone/, /^};$/ {split($0, a, "\""); print > a[2] }'

LG
From: John W. Krahn 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"' ?

perl -pe'BEGIN{ $/ = "};\n" } /zone "([^"]+)"/ and open STDOUT, ">>",
$1' input.file



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
From: Janis Papanagnou on
Laurianne Gardeux wrote:
> Rocky Jr. à écrit :
>
>> 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?

In case I'd above have misunderstood the actual filename the OP actually
wants, please note that...

>
>
> awk 'BEGIN { RS="" }/^zone/, /^};$/ {split($0, a, "\""); print > a[2] }'

....it's better to close() the unused open file handles, and you can also
simplify the code using the builtin field splitting...

awk 'BEGIN { RS="" ; FS="\"" } /^zone/, /^};$/ { print > $2 ; close($2) }'


Janis

>
> LG
From: Laurianne Gardeux on
Janis Papanagnou à écrit :

> Laurianne Gardeux wrote:

>> awk 'BEGIN { RS="" }/^zone/, /^};$/ {split($0, a, "\""); print > a[2]
>> }'
>
> ...it's better to close() the unused open file handles, and you can also
> simplify the code using the builtin field splitting...
>
> awk 'BEGIN { RS="" ; FS="\"" } /^zone/, /^};$/ { print > $2 ; close($2)
> }'
>
>
> Janis

Oh yes, thank you !

LG