-1

I'm using the following command with pdf to search for specific strings inside several pdfs, and move for a target directory, if there is a match:

pdfgrep -H "DESIRE STRING" TARGET/* | grep --ignore-case --perl-regexp --only-matching '.*(.PDF)'| uniq | xargs -I{} mv -i {} DESTINATION/

I would like to print ok if a match is found, or not found if there isn't a match. Is it possible to include this in command line?

Thanks.

1 Answer 1

1

Is there some reason you are trying to do this in a one-liner command? It is complicated enough that it would be easier to manage as a small shell script. But, if you want a one-liner, here is a sample that is basically a bash loop that calls pdfgrep, based on your example:

while read line; do file=$(echo $line|awk -F: '{print $1}');printf "$file: "; echo "$line"|grep -q :0$ && echo no match && continue;echo MATCH;mv -i "$file" DESTINATION/;done < <(find TARGET/ -type f -iname '*.pdf' -exec pdfgrep -Hc -m 1 "DESIRE STRING" {} \;)

Note that pdfgrep uses the "-c" option to determine whether or not the text is in the PDF file. It uses the "-m 1" option to stop after the first match, so should in theory run faster on large files.

If your pdfgrep does not support the "-m 1" option, leave that bit off.

And for easier readability, here is the code as it might appear in a bash script:

#!/bin/bash
while read line; do
  file=$(echo $line|awk -F: '{print $1}')
  printf "$file: "
  echo "$line"|grep -q :0$ && echo no match && continue
  echo MATCH
  mv -i "$file" DESTINATION/
done < <(find TARGET/ -type f -iname '*.pdf' -exec pdfgrep -Hc "DESIRE STRING" {} \;)

Code explanation, line by line:

  1. This is the command line interpreter. If the script were executable (you can make it executable with the command "chmod +x mv-pdf-files.sh"), you could run it like "./mv-pdf-files.sh" and it would use /bin/bash to run the script.

  2. This is a bash while loop. It iterates over every line of output generated by the command at the very end of the script (the "find" command)

  3. This grabs the filename from the line of output and saves it to a variable (named "file").

  4. This prints the filename to Standard Output, without a new line at the end.

  5. This grep command looks for ":0" at the end of the line, which indicates that zero matches of the STRING were found in that PDF. The && commands are chained together; so if no match is found, then the echo command is run, and then a bash "continue" is called, which skips to the next file in the loop (i.e., goes back to code line #3).

  6. This just echoes that a match was found.

  7. This is the actual mv command, same as in your code.

  8. The "done" indicates the end of the while loop. The "find" command inside the < <( ) notation is what the while loop is iterating over. The find command itself simply finds all files in the path specified (TARGET/) that end in .pdf (case-insensitive). The -exec parameter tells find to run the "pdfgrep" command on each file, using -H to print the filename and "-c" to print the number of matches of the STRING found in the PDF file. The {} characters at the end are used internally by "find" to represent the filename as a variable.

6
  • No, i could use a bash script, i just don't know how hahaha.
    – FXux
    Commented Mar 28, 2016 at 23:14
  • I'm seeing 'invalid option -m' in pdfgrep. Also, if i'm not asking to much, can u explain the command line?
    – FXux
    Commented Mar 28, 2016 at 23:30
  • Isn't working right @atreyu, is moving files that don't have the string inside it :/.
    – FXux
    Commented Mar 28, 2016 at 23:43
  • ah, you have an older pdfgrep, I guess. I found a machine of mine w/a an old version, too. My fault for not including error checking. Just leave off the "-m 1" option and it should work. Updated the answer to show the commands in a script format. Just open a text editor and save the code to a file ("mv-pdf-files.sh", e.g.) and then call it like: "sh mv-pdf-files.sh". You could get fancy and pass the src/dest dirs as args to the script, and pass the string to search on as an arg, too.
    – atreyu
    Commented Mar 29, 2016 at 3:09
  • OH MAN, IS WORKING <3. If i'm not asking to much, could you explain the script? I love bash, but i don't have much time to learn, at least in this year.
    – FXux
    Commented Mar 29, 2016 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .