Skip to content

Instantly share code, notes, and snippets.

@jdingel
Last active July 10, 2019 20:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdingel/29e27960e9f60e1a67960e54eaa0b0af to your computer and use it in GitHub Desktop.
Save jdingel/29e27960e9f60e1a67960e54eaa0b0af to your computer and use it in GitHub Desktop.
Shell script to automatically rename downloaded NBER WP PDFs
#!/bin/bash
#This script searches the Downloads folder for NBER working papers (PDFs starting with "w2") and renames them in "Author - Title (NBER Year)"" format.
cd ~/Downloads
papers=$(find . -name "w2[0-9]*.pdf" | sed 's/^.\//\ /' | sed 's/.pdf//' | tr -d '\n')
echo "$papers"
for wp in $papers
do
curl https://www.nber.org/papers/$wp.ris > temp.txt
grep 'AU' temp.txt | awk -F- '{print $2}' | awk -F, '{printf $1 ","}' > temp2.txt
echo ' - ' >> temp2.txt
grep 'TI' temp.txt | cut -d '-' -f 2- >> temp2.txt
grep 'PY' temp.txt | awk -F- '{print " (NBER" $2 ") "}' >> temp2.txt
grep 'VL' temp.txt | awk -F. '{print "w" $2".pdf"}' | sed 's/\ //' >> temp2.txt
cat temp2.txt | tr -d '\n' | sed 's/:/ -/g' | sed 's/, -/ -/' | sed 's/\ \ /\ /g' | sed 's/^\ //' > temp3.txt
if [ "$(cat temp3.txt | wc -c)" -ge 15 ]; then mv $wp.pdf "$(cat temp3.txt)" ; fi
rm temp.txt temp2.txt temp3.txt
done
@louisdecharson
Copy link

Hi,
I have spotted your script via econ' folks on Twitter and during a long commute, I have tried to come up with a version without using temporary files 😄

#!/bin/bash
# This script searches for NBER working papers (PDFs starting with "w2") and renames them in "Authors - Title (NBER Year)"" format.
# Arguments :
# $1 = folder to be searched. If left empty, look into Downloads
if [ -z $1 ]; then folder="~/Downloads"; else folder="$1"; fi
function getMoveCommand {
    while read data; do
        curl "http://www.nber.org/papers/${data}.ris" | awk -v folder="${folder}" -v quote="'" -F"  - " '{if ($1=="AU") {sub(/,.*/,"",$2); authors=authors", "$2} else if ($1=="TI"){title=$2} else if ($1=="PY") {year=$2} else if ($1=="L1") {gsub(/.*\//,"",$2); file=$2}} END {sub(/^, /,"",authors); print "mv "folder"/"file" "quote""folder"/"authors" - "title" ("year").pdf"quote}'
    done
}
find "${folder}" -name "w2[0-9]*.pdf" | sed 's/.pdf//' | awk -F"/" '{print $NF}' | getMoveCommand | bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment