You can do this with a very simple state machine - only two states: header or body.
curfile != FILENAME{body=0;curfile=FILENAME}
!body && /^\/\/$/ {body=1}
body && /^\[/ {print > "first_"FILENAME}
body && /^(seg|pos)/{print > "second_"FILENAME}
body && /^[01]+/ {print > "third_"FILENAME}
This starts by setting body
to zero/false whenever the filename changes (curfile
will initially be unset), and switches that to one/true when the header separator is seen. The other rules only apply inside the body.
To extract the first bracketed number from the first group of lines, with this simple pattern you can just use the substr
and index
string manipulation functions. Something like the following should do:
body && /^\[[0-9]+\]/ {
print > "first_"FILENAME
print substr($0, 2, index($0,"]")-2) > "fourth_"FILENAME
}