1

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.

2
  • 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. Commented Oct 21, 2016 at 17:21
  • For this particular application, the online documentation suggests that wkhtmltopdf provides a --read-args-from-stdin option that may be useful Commented Oct 21, 2016 at 17:32

2 Answers 2

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'

2
  • Thanks. What does $* my_$$.pdf mean? Commented Oct 21, 2016 at 17:41
  • Cool (+1) xargs is always a source of surprises for me :) Commented Oct 21, 2016 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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.