Skip to content

Instantly share code, notes, and snippets.

@louisdecharson
Forked from jdingel/NBERwp_rename.sh
Last active January 25, 2019 00:59
Show Gist options
  • Save louisdecharson/2fbba9d3edb00c0d05288561443c3b6b to your computer and use it in GitHub Desktop.
Save louisdecharson/2fbba9d3edb00c0d05288561443c3b6b to your computer and use it in GitHub Desktop.
Shell script to automatically rename downloaded NBER WP PDFs
#!/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