How can I use the output of ls/grep as input for pdfunite

0

I want to use pdfunite to merge several pdf-files into one output file. Since the directory contains lots of different stuff, I thought i could use ls/grep to get the apropriate file-names and then use pdfunite to get it put together. The output of ls/grep look like this:

ls | grep "handout...pdf" 
handout01.pdf*
handout02.pdf*
handout03.pdf*
handout04.pdf*
handout05.pdf*
handout06.pdf*

Then I want to invoke pdfunit, which has the following syntax:

 pdfunite [options] PDF-sourcefile1..PDF-sourcefilen PDF-destfile

My Problem is, that it does not seem like I can pipe the output of ls/grep into pdfunite, because it is not supposed to work like that. On the otherhand, if I try:

pdfunite ls | grep "handout...pdf" grep.pdf

then grep obviously complains about my chosen output-file:

grep: grep.pdf: No such file or directory

I know I could do two seperate steps and then it would work, but I'd like to figure out how to do this with one line.

achnichtsowichtig

Posted 2015-05-20T18:25:30.990

Reputation: 3

Answers

2

If your file patterns are that simple, you don't really need a regex, and you can use a simple wildcard:

pdfunite handout??.pdf

Otherwise, you can use a regex with the find command and -regex, but I could not find a way to execute it that doesn't have the possibility of splitting up into multiple groups of files. Some of the ways you might do it also depend on none of the filenames having any spaces in them.

Random832

Posted 2015-05-20T18:25:30.990

Reputation: 601

1

the "tick" character can be used to do that (the thing above the tilda), I believe. Try something like this:

pdfunite [options] `ls | grep "handout...pdf"`

From what I understand, that's how you nest a command. Anything inside the ticks is executed as its own command, and the output of it replaces the ticks. So, the command above would essentially equate to

pdfunite [options] handout01.pdf* handout02.pdf* ...

Russell Uhl

Posted 2015-05-20T18:25:30.990

Reputation: 433

True. But using ls for this is really evil (though common). You have no guarantee that the output of ls will be consistent. It often is aliased. Find otoh is consistent. – Hennes – 2015-05-20T18:41:17.577

True that. I wanted to provide an answer using his original attempts, however, so I didn't use find – Russell Uhl – 2015-05-20T18:42:56.347

1Oh, it is a good answer for the OP. I just wanted to state that for all the other people who will eventually read this. – Hennes – 2015-05-20T19:01:12.297

0

You don't necessarily need a grep. You can pass on the pattern to ls.

pdfunite $(ls handout*.pdf*) merged.pdf

rahul

Posted 2015-05-20T18:25:30.990

Reputation: 101

There's no guarantee that this executes pdfunite only once – Random832 – 2015-05-20T18:33:22.330

@Random832 Sorry i misread the question. Yes this will execute the pdfunite for each of the pdf files. – rahul – 2015-05-20T18:36:44.097

@Random832 . I've edited the answer to suit the question now. Thanks for pointing it out. – rahul – 2015-05-20T18:43:43.200