From: Rocky Jr. on 9 May 2010 16:40 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 9 May 2010 16:50 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 9 May 2010 16:55 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 9 May 2010 17:26 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 9 May 2010 17:36 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
|
Next
|
Last
Pages: 1 2 Prev: Exit status of process by PID Next: Running a script through Automatic scheduler |