Add missing linefeeds between formfeeds in reports

I need to take a report text file that is output from vendor software and there are some pages which have less then the normal amount of lines. I need to add these missing line feeds as there is a merge program that reads this file in fixed character and line mode template to output the final PDF report.
To state more simply there is a text file with 65 lives beteen each form feed and then randomly there is a page with 64 lines so I need to add in the missing line feed.

I think awk can do this. Set the record separator as form-feed, and set the field separator as newline, so every 'line' is an entire page, and every 'field' is an entire line.

NF thus becomes the count of lines. And you can just set NF, then print. It will happily fill linefeeds between blank fields.

Requires nawk or gawk unless you're on Linux.

# Proof of principle.  Each 'block' separated by form-feed(\014)
# gets stretched to 5 lines.  Form-feed is not printed so the terminal doesn't erase itself.
printf "a\n\014b\n\014c\nd\ne\n\014" | awk -v RS="\014" -v FS="\n" -v OFS="\n" -v ORS="\n" '{ NF=5 } 1'
a




b




c
d
e


# Command you'd actually run:
$ awk -v RS="\014" -v FS="\n" -v OFS="\n" -v ORS="\n\014" '{ NF=65 } 1' input > output

If your document uses carriage returns, change FS, OFS, and ORS so \n is \r\n.

In addition: awk should know RS='\f'

when I run in HPUX

HP-UX FRHGLIS1@FRH:/sunquest/lab/reports/hpf/SAVE # awk -v RS="\014" -v FS="\n" -v OFS="\n" -v ORS="\n\014" '{ NF=65 } 1' in > out
awk: Input line                      cannot be longer than 3,000 bytes.
 The source line number is 1.
HP-UX FRHGLIS1@FRH:/sunquest/lab/reports/hpf/SAVE #

So there must be more than 300 bytes per page or the formfedd RS is not recognized
same with "\f"

HP-UX FRHGLIS1@FRH:/sunquest/lab/reports/hpf/SAVE # awk -v RS="\f" -v FS="\n" -v OFS="\n" -v ORS="\n\014" '{ NF=65 } 1' in > out
awk: Input line                      cannot be longer than 3,000 bytes.
 The source line number is 1.

Some UNIX systems keep mouldy old pre-nawk versions of awk around for compatibility reasons. But they almost always have the real deal available as nawk.

Which is why I said "requires nawk or gawk". :wink:

I use Linux, which doesn't have any 'nawk' executable at all, since the general-purpose awk for GNU (aka GNU awk, aka gawk) is nawk-compatible.

awk on HPUX is POSIX compliant and so should be based on nawk. Some awks for some reason need to have the fields recalculated first ( $1=$1 ). Combining this with Corona688's suggestion, try this:

awk '{$1=$1}NF=65' FS='\n' OFS='\n' RS='\f' ORS='\f'

I did not use a newline in ORS, since I think 65 lines per pages, should mean 64 newlines characters before the form feed, no?

-- edit --
OK, I noticed that the message indicates the the record can not be longer than 3000, that means we probably cannot use a form feed as a record separator for that reason...

Try this:

awk '{n++} /^\f/{for(i=n;i<p;i++)print x; n=1}1' p=65 infile