How do I get a comma-separated list of all color pages in my PDF file?

Thanks to this answer I can get a list of all Page numbers and a note for a color page.

gs -o - -sDEVICE=inkcov input.pdf | grep -v "^ 0.00000  0.00000  0.00000" | \
   grep "^ \|^Page"

The output would be:

Page 1
 0.16646  0.16261  0.14122  0.13507 CMYK OK
Page 2
Page 3
 0.48488  0.48728  0.48794  0.16678 CMYK OK
Page 4
 0.47346  0.47703  0.47624  0.46490 CMYK OK
Page 5

How do I modify the output to return just the pages with color in a comma-separated list?

The result should looks something like that:

1, 3, 4

Okay I find a way to generate a comma-separated list of the color pages in a PDF:

gs -o - -sDEVICE=inkcov test.pdf | \
  grep -v "^ 0.00000  0.00000  0.00000" | \
  grep "^ \|^Page" | \
  paste -sd, | \
  grep -Eo '[0-9]{1,3}, ' | \
  tr -d '\n'

Probably not the most elegant way, but it works.

The code does the following:

  • takes the same output like in the question above and replaces the new lines with a comma

    paste -sd,
  • than it look for numbers between 1-999 followed by a comma (the repalced new line) and an empty space, as the color note always starts with an empty space sign

    grep -Eo '[0-9]{1,3}, '`
  • at the end it removes ones again the new lines from the output.

    tr -d '\n'
  • The result would be:

    1, 3, 4,

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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