將文件中的行分組並一次將一組輸入到程式中

將文件中的行分組並一次將一組輸入到程式中

我有一個程序,它將 url 作為命令列參數並輸出一個 pdf 文件。例如,我使用命令替換來提供文件中的輸入 urlurlsfile

wkhtmltopdf $(cat urlsfile) my.pdf

在 中urlsfile,每一行都是一個 url。

現在我想將 中的每 15 行進行分組urlsfile,並一次向程式提供一組 url。我怎樣才能在 bash 中做到這一點?

請注意,每 15 個網址建立一個 pdf 檔案是可以接受的,然後我會將這些 pdf 檔案合併為一個。如果合併可以由程式完成,那就更好了。

謝謝。

答案1

xargs

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

或者如果你xargs不支持-a

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

答案2

gawk        '{ f=f " " $0} 
    NR%15==0 { print("wrhtml2pdf " f " " NR/15 ".pdf") ; f=""}' urls

如果您喜歡輸出,請替換printsystem

相關內容