How to format response of a bash command?

Asked

Viewed 55 times

1

My intention is to print on the screen the number of pages of each PDF in a given directory. I even managed to do this through the following command:

find "$PWD" -iname "*.pdf" | xargs -i pdfinfo {} | grep Pages | sed 's/[^0-9]*//'

However, the answer to this command is only the number of pages of each PDF, one in each line.

My question is: how to format the above command response in order to print the name of each PDF file along with its number of pages, as follows:

nome_do_arquivo1.pdf : 230
nome_do_arquivo2.pdf : 123

1 answer

2


A one-line solution is to use the for to iterate on all files *.pdf of the current directory, giving a echo in the file name itself and concatenating the : with the return of the number of pages.

Example:

$ for f in *.pdf; do echo $f : `pdfinfo $f | grep Pages | sed 's/[^0-9]*//'`; done
Shakespeare-A-comedia-dos-erros.pdf : 106
Shakespeare-a-tempestade.pdf : 119
Shakespeare-Hamlet.pdf : 192

Browser other questions tagged

You are not signed in. Login or sign up in order to post.