Last active
July 10, 2019 20:51
-
-
Save jdingel/29e27960e9f60e1a67960e54eaa0b0af to your computer and use it in GitHub Desktop.
Shell script to automatically rename downloaded NBER WP PDFs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 😄