Awk: Page number with total number of pages, EG Page 1 of 5

So I've worked how to add page numbers based on regex. It's using the footer text.

How do we get the total amount added so we have page number with the total number of pages?

Desired output: Page No:1 of 5

Thanks in advance.

process the file twice: first for calculating the total, and the second to print your summary.

Thanks for this, this works but it looks like the whole document is being repeated?

#!/usr/local/bin/awk -f
#
#

BEGIN {FS=";"}

/This is the last line of the page/{a++;$0=$0"\n                          Page No:"a}1
/This is the last line of the page/{a++;$0=$0"\n                          Page Total:"a}1

Hmmm... I don't quite understand the code and this is NOT what I meant...

awk -F';' '

FNR== NR && /This is the last line of the page/{ total++;next}
/This is the last line of the page/{printf("Page No:%d of %d\n", ++page, total)}' textfile1  textfile1  > textfile12

I'll let you work out the exact formatting as you want...

Thanks for this.

Can you please explain the last line of code using the original file twice before the last output?

Using your snippet solves the issue although the final outfile only has the page numbers in a file but nothing else.

# when a string "This is last..." is found, print the CURRENT page count (page) incremented by 1 (++) and the total # of pages (total)
/This is the last line of the page/{printf("Page No:%d of %d\n", ++page, total)' textfile1 textfile1 > textfile12

As I noted, you'll have to format the final print out based on your desired format.
It's hard understand what's your desired output should look like...
What is it?

Thanks again,

That string is actually the last line in a report print. It's a footer so every single page will have that line. The idea was to insert page numbers "Page 1 of X" on the next line under the footer string, counting each string as one page, it will only appear once per page.

Ah, ok.
How 'bout:

FNR== NR && /This is the last line of the page/{ total++;next}
/This is the last line of the page/{printf("%s\nPage No:%d of %d\n", $0, ++page, total);next}1' textfile1  textfile1  > textfile12

That's perfect almost!

For some reason, the output file is inserting the original 3 pages without that last line followed by the desired output of the 3 pages with the page numbers. So for 3 pages of data we have 6 pages.

What is happening with having the 2 input files as argument before the output file?

textfile1 textfile1 > textfile12

we're parsing input file testfile1 TWICE: one for calculating the total number of pages AND second for inserting the No:N of M
For the missing numberings for the pages 1 through 3, you must either have the footer missing OR the footer line is somehow different than the one on the other 3 pages where the totals are printed.
Make sure IF the footers are present they are formatted EXACTLY as expected - word by word, space by space....

Hi vgersh99,

They should be the same as there are only 3 pages in total in my sample file. It looks like the final output has the original 3 pages without the string "This is the last line of the page" with no page numbers followed by the same 3 pages with the string including the page numbers.

Sorry, my bad:

awk -F';' '
  FNR==NR { if (/This is the last line of the page/) total++;next}
  /This is the last line of the page/{printf("%s\nPage No:%d of %d\n", $0, ++page, total);next}1' textfile1  textfile1  > textfile12

Hi vgersh99,

Yes, it works perfectly now!

I wonder if one should consolidate to one lengthy condition?
Here is my attempt

awk -F';' '
  FNR!=NR
  /This is the last line of the page/ {
    if (FNR==NR) total++; else printf "Page No:%d of %d\n", ++page, total
  }
' textfile1  textfile1  > textfile12

@MadeInGermany: Wouldn't that print $0 twice, once for each of the conditions?

1 Like

You are right. In the post #12 I missed the other next that skips the following 1 (always true and default print).
In fact it prints the page count after the last line. So the FNR!=NR (default print if 2nd file) must be first. I have corrected my previous post (again).