1

I'm trying to rename a bunch of pdfs based on a specific line in the files.

So far, I'm converting to text, then using awk to print line 41. But that's as far as I've got.

I've tried usings xargs to pass the awk output to a mv command, but no dice to date.

I know I'm probably going about this the wrong way, but if anyway can shed some light I'd really appreciate it.

I'm trying to this without bash, so that I can make an alias of it btw.

ls *.pdf | xargs -n1 mv `ls *.pdf | xargs -n1 pdftotext; ls page_*.txt | xargs -n1 awk 'NR==41'`.txt'
2

You have several options to choose from. You can write a function to do this, or you can write a (bash) shell script which you also can use within alias. And of course you can try to write a one-liner to do the magic.

I think of a command in the following structure (as one-liner):

for pdf in *.pdf; do pdftotext "${pdf}"; filename=`basename -s .pdf "${pdf}"`; newname=`awk 'NR==41' "${filename}.txt"`; mv "${pdf}" "${newname}"; done;`

Which can be restructures as:

for pdf in *.pdf
do
    pdftotext "${pdf}"
    filename=`basename -s .pdf "${pdf}"`
    newname=`awk 'NR==41' "${filename}.txt"`
    mv "${pdf}" "${newname}"
done

This file can be saved as shell script and added to the aliases list under any name you like.

4
  • Hi Thanks for your reply. This looks really close. I'm getting an error, which I think has to do with basename -s. ---- basename: invalid option -- 's' Try basename --help' for more information. awk: cmd. line:1: fatal: cannot open file .txt' for reading (No such file or directory) mv: cannot move page_01.pdf' to ': No such file or directory – byrnec25 Mar 24 '15 at 13:29
  • 1
    Sorry, my bad, I forgot the end backticks on the filename and newname lines. Besides, I have basename version 8.21 – Lambert Mar 24 '15 at 13:31
  • If you do not have the -s in your basename you can try to substitute the line with: `filename="${pdf::-4}" which substracts the last four characters (.pdf) from the PDF filename. – Lambert Mar 24 '15 at 13:36
  • Thanks, I got it working using basename "${pdf}" ".pdf". This looks like it's gonna work for me. Thanks again. – byrnec25 Mar 24 '15 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.