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:
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.
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)
This grabs the filename from the line of output and saves it to a variable (named "file").
This prints the filename to Standard Output, without a new line at the end.
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).
This just echoes that a match was found.
This is the actual mv command, same as in your code.
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.