[Solved] awk command to read sequentially from a file until last record

Hello, I have a file that looks like this:

Generated geometry (...some special descriptor)
1  0.56784  1.45783  -0.87965
8  1.29873  -0.8767  1.098789
...  ...            ...           ...
Generated geometry (....come special descriptor)
...   ....   ...   ...
...    ...   ...   ...

and it continues like that... I'm pretty sure there is a way (probably a while loop with read command within awk) to read the first set of data after the first "Generated geometry" statement, redirect it to an output file, then proceed to read the next set of data after the second "Generated geometry" statement and redirect it to another output file, and so on until the last set of data (after the last "Generated geometry statement").
Can somebody help me with this? I have tried using the "Generated geometry" statement as flags, and trying to print between flags, but since there are too many of those statements it gets all messed up.
Thank you,

awk 'BEGIN{ cnt=1} 
                  /Generated/ {ofile=sprintf("file%04d", cnt++) ; next}
                  /Generated/ && FNR > 1 {close(ofile)}
                  {print $0 > ofile) } '  inputfile

This produces files: file0001, file0002...file9999. See if that works for you.

1 Like

Thanks for your reply. It seems to work very well, for the first 17 files file0001-file0017 (they are supposed to be 21 in total), but I get the following error message, which I beleive might be for the memory usage or something like that:

awk: file0018 makes too many open files
input record number 172, file tempfile7
source line number 4

Actually, it means exactly what it says... Too many open files. The close() isn't working for some reason. Memory usage is a nonissue, since we're not actually storing anything in memory.

I think the close should come first, otherwise it will never close the previous file, just the current one (which gets immediately reopened).

awk '/Generated/ && ofile {close(ofile)}
                  /Generated/ {ofile=sprintf("file%04d", ++cnt) ; next}
                  {print $0 > ofile) } '  inputfile
1 Like

It worked just perfect! :slight_smile: