首页 > 解决方案 > How to get a comma-separated list of pages with color in a PDF?

问题描述

Consider this PDF 'colorpages.pdf' linked here

I have two commands to know a) how many colored pages I have in my PDF b) on which page color is used.

For a) I use

gs -o - -sDEVICE=inkcov colorpages.pdf | grep -v "^ 0.00000  0.00000  0.00000" | grep "^ " | wc 

and I get back a number (total amount of pages with color).

For b) I use

gs -o - -sDEVICE=inkcov  colorpages.pdf |tail -n +5 |sed '/^Page*/N;s/\n//' |sed -E '/Page [0-9]+ 0.00000 0.00000 0.00000 / d'

which gives me a nice list with color information; e.g.

Page 1 0.00000  0.00000  0.00000  0.99454 CMYK OK
Page 2 0.00000  0.99228  0.00000  0.00000 CMYK OK
Page 3 0.99222  0.00000  0.00000  0.00000 CMYK OK
Page 4 0.99289  0.99289  0.99289  0.99289 CMYK OK
Page 5 0.99316  0.99316  0.00000  0.00000 CMYK OK
Page 6 0.99130  0.99130  0.99130  0.99130 CMYK OK
Page 7 0.00000  0.00000  0.00000  0.99392 CMYK OK
Page 8 0.00000  0.00000  0.00000  0.99189 CMYK OK
Page 9 0.00000  0.00000  0.00000  0.99128 CMYK OK
Page 10 0.00000  0.00000  0.98594  0.00000 CMYK OK
Page 11 0.00000  0.98908  0.98908  0.00000 CMYK OK
Page 12 0.00000  0.00000  0.00000  0.98682 CMYK OK

But what I need (additionally) is a comma separated list of all the pages color is used.


Following up

The command

gs -o - -sDEVICE=inkcov colorpages.pdf |tail -n +5 |sed '/^Page*/N;s/\n//' |sed -E '/Page [0-9]+ 0.00000 0.00000 0.00000 / d' | awk '$3!=0 && $4!=0 && $5!=0{if(length(colored))colored=colored","$2;else colored=$2} END{print colored}'

gives me the following list 1,2,3,4,5,6,7,8,9,10,11,12. But it should be 2,3,4,5,6,10,11.

标签: listpdfsedgrepghostscript

解决方案


awk '$3!=0{if(length(colored))colored=colored","$2;else colored=$2} END{print colored}'
2,5,7




 -->echo "$x"
Page 1 0.00000  0.00000  0.00000  0.12395 CMYK OK
Page 2 0.00016  0.00035  0.00017  0.47061 CMYK OK
Page 3 0.00000  0.00000  0.00000  0.34571 CMYK OK
Page 4 0.00000  0.00000  0.00000  0.32637 CMYK OK
Page 5 0.00016  0.00035  0.00017  0.47061 CMYK OK
Page 6 0.00000  0.00000  0.00000  0.63394 CMYK OK
Page 7 0.00016  0.00035  0.00017  0.47061 CMYK OK


echo "$x" |awk '$3!=0{if(length(colored))colored=colored","$2;else colored=$2} END{print colored}'
2,5,7

如果 col 3,4,5 需要0调用有颜色的行,那么:(你可以玩弄这些条件)

awk '$3!=0 &&  $4!=0 && $5!=0{if(length(colored))colored=colored","$2;else colored=$2} END{print colored}'

更新

您可以使用 OR 运算符(||)来获得问题中提到的所需结果。

echo "$x" |awk '$3!=0 ||  $4!=0 || $5!=0{if(length(colored))colored=colored","$2;else colored=$2} END{print colored}'
2,3,4,5,6,10,11

推荐阅读