Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 http://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
mv $wp.pdf "$(cat temp3.txt)"
rm temp.txt temp2.txt temp3.txt
done
@louisdecharson

This comment has been minimized.

Show comment
Hide comment
@louisdecharson

louisdecharson Oct 15, 2018

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

louisdecharson commented Oct 15, 2018

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