0

I have a program which takes urls as commandline arguments and output a pdf file. For example, I use command substitution to provide the input urls from a file urlsfile

wkhtmltopdf $(cat urlsfile) my.pdf

In urlsfile, each line is a url.

Now I would like to group every 15 lines in urlsfile, and feed a group of urls to the program at a time. How can I do that in bash?

Note that it is acceptable to create a pdf file per 15 urls, and then I will merge the pdf files into one. If the merge can be done by the program, that is better.

Thanks.

  • cat urlsfile | xargs wkhtmltopdf my.pdf doesn't work, because I only know the program takes inputs from cmdline args, and not sure how to make it take inputs from stdin. – Tim Oct 21 '16 at 17:21
  • For this particular application, the online documentation suggests that wkhtmltopdf provides a --read-args-from-stdin option that may be useful – steeldriver Oct 21 '16 at 17:32
4

With xargs:

xargs -a urlsfile -n 15 bash -c 'wkhtmltopdf "$@" my_$$.pdf'

or if your xargs doesn't support -a:

cat urlsfile | xargs -n 15 bash -c 'wkhtmltopdf "$@" my_$$.pdf'

  • Thanks. What does $* my_$$.pdf mean? – Tim Oct 21 '16 at 17:41
  • thanks @ilkkachu "$@" is the right one – FloHimself Oct 21 '16 at 17:44
  • Cool (+1) xargs is always a source of surprises for me :) – JJoao Oct 21 '16 at 18:06
2
gawk        '{ f=f " " $0} 
    NR%15==0 { print("wrhtml2pdf " f " " NR/15 ".pdf") ; f=""}' urls

and if you like the output, replace print by system

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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