Reputation: 20448
I'm trying to get the number of pages in a PDF file through the command line.
pdfinfo "/tmp/temp.pdf" | grep Pages: | awk '{print $2}'
produces
3
In Node.js, I need to use 'sh' because of the piping.
But
sh -c "pdfinfo '/tmp/temp.pdf' | grep Pages: | awk '{print $2}'"
produces
Pages: 3
Why am I getting different output?
Upvotes: 0
Views: 44
Reputation: 14510
The sh -c
command is double quoted, which will expand the $2
which is likely empty, so the awk
command becomes just print
which prints the whole line. You could escape the $
to prevent it from being expanded:
sh -c "pdfinfo '/tmp/temp.pdf' | grep Pages: | awk '{print \$2}'"
Incidentally, awk
can do pattern matching, so no need for both grep
and awk
:
pdfinfo "/tmp/temp.pdf" | awk '/Pages:/ {print $2}'
Upvotes: 2