Skip to content

Instantly share code, notes, and snippets.

@niflostancu
Created May 21, 2019 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niflostancu/2947dba921f54b060d14119ddb5741f8 to your computer and use it in GitHub Desktop.
Save niflostancu/2947dba921f54b060d14119ddb5741f8 to your computer and use it in GitHub Desktop.
#!/bin/bash
NAMES_CSV="$(realpath "$2")"
set -e
cd "$1"
for dir in *; do
if [[ "$dir" =~ _([0-9]+)_ ]]; then
ID=${BASH_REMATCH[1]}
NEWNAME=$(cat "$NAMES_CSV" | grep -E "^[a-zA-Z]+$ID," | head -1 | awk -F',' '
BEGIN {
FPAT = "([^, ]+)|(\"[^\"]+\")"
} {
if (substr($2, 1, 1) == "\"") {
len = length($2)
$2 = substr($2, 2, len - 2) # Get text within the two quotes
}
printf "%s-%s", $2, $3
}')
NEWNAME=${NEWNAME%@*}
NEWNAME=${NEWNAME##* }
echo "$dir => $ID :: $NEWNAME"
mv "$dir" "$NEWNAME"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment