0

I have a large text file, containing up to 2300 sections. Each section begins, one line before, "PAGE 1" The page number is at very end of the line,

Example:

T5271-AX1   TITLE                             DATE
     SUB TITLE                                PAGE   1
LINE1
LINE2                                
T5271-AX1   TITLE                             PAGE   2
LINE3
LINE4
     SUB TITLE                                PAGE   3
LINE5
T5271-BX1                                     DATE
     SUB TITLE                                PAGE   1
LINE1

The file up to 2300 sections and each section can have several hundred pages, but sections always has "PAGE 1" at the end of the line.

The name of the file is contained in the first string of 9-15 characters on the line above "PAGE 1"

I want to split the file at "PAGE 1" and used the 9-15 characters at the name of the output file.

In my example the end result would be two files:

T5271-AX1
T5271-BX1

I can easily locate the page number and the title, but I don't know how to split the file and output it into a new file, until the the the next occurance of "PAGE 1".

Any help would be appreciated

1
  • There is no PAGE 1 in your input ...
    – guest_7
    Aug 4 '21 at 20:51
5
$ cat tst.awk
/ PAGE   1$/ {
    close(out)
    split(prev,p)
    out = p[1]
}
NR > 1 { print prev > out }
{ prev = $0 }
END { print prev > out }

$ awk -f tst.awk file

$ head T*
==> T5271-AX1 <==
T5271-AX1   TITLE                             DATE
     SUB TITLE                                PAGE   1
LINE1
LINE2
T5271-AX1   TITLE                             PAGE   2
LINE3
LINE4
     SUB TITLE                                PAGE   3
LINE5

==> T5271-BX1 <==
T5271-BX1                                     DATE
     SUB TITLE                                PAGE   1
LINE1
0

The command csplit , for context split, will do what you are asking for.

csplit -sz file '/ PAGE   1$/-1' '{*}'
for  f in xx*;do
  mv -f -- "$f" "$(< "$f" head -n1 | cut -f1 -d" ")"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.