Split a text file into multiple pages based on pattern

Hi,

I have a text file (attached the sample). I have also, attached the way the way the files need to be split.
We get this file, that will either have 24 Jurisdictions, or will miss some and retain some.
Like in the attached sample file, there are only Jurisdictions 03,11,14,15, 20 and 30.
For all the Jurisdictions in the file, i should split based on the Jurisdiction number. Some scenarios (like Jurisdiction 15), a jurisdiction can span multiple pages. In that scenario, it should retain all those pages into a separate file.

and these file names should be appended with the Jurisdiction number, so that they can be identified.
Please help.

Why not look for something that will always appear in the first line and insert a form feed character at the beginning of the line?

The criteria for a new file is the string JURISDICTION NUMBER:
No problem if this is the first line in the new file.
But you want already the previous line to go to the new file.
Here is a general solution with a backlog (ring-)buffer.

awk '
BEGIN {
  # backlog buffer length
  b=1
  # backlog buffer will be B[0]...B[b-1]
  # NR is the current line number
  # NR%b cycles thru 0...(b-1)
}
($1=="JURISDICTION" && $2=="NUMBER:") {
 # new output file
 ofile="Report-"$3".txt"
}
(NR>b) {
 # print the last line of the backlog buffer, but not for line 1...(b-1)
 print B[NR%b] > ofile
}
{
  B[NR%b]=$0
}
END {
  # print the outstanding lines from the backlog buffer
  for (i=1; i<=b; i++) print B[(NR+i)%b] > ofile
}
' Report-02.txt

Awesome!! Thanks! that was wonderful :slight_smile: