Skip to content

Instantly share code, notes, and snippets.

@zendeavor
Created July 6, 2012 04:46
Show Gist options
  • Save zendeavor/3058164 to your computer and use it in GitHub Desktop.
Save zendeavor/3058164 to your computer and use it in GitHub Desktop.
fix this damn thing
#!/bin/bash
## usage: ./script broken_sql_file path_to_images_toplevel
## `./script /foo/bar/omg_broken_imgpaths.sql /baz/quux/images/`
## a line number is optional as final argument to pick from
## the middle of the file instead of always starting over
shopt -s extglob
shopt -s nocasematch
files_path="$1"
broken="$2"
line_no="$4"
fixed="${broken}-fixed"
[[ -f "${broken}-backup" ]] || cp -f "$broken" "${broken}-backup"
declare -a jpegs jpgs jpes pngs loose odd pattarr
while read -r files; do
img="${files#*2012/}"
case "$files" in
*+(jpe+(g))*) jpegs+=("$files") ;;
*+(jpg)*) jpgs+=("$files") ;;
*+(jpe!(g))*) jpes+=("$files") ;;
*+(png)*) pngs+=("$files") ;;
*+(mlk|bmp|amerdave)*) loose+=("$files") ;;
*) odd+=("$files") ;;
esac
done < <(find "$files_path" -type f)
match='.*05/.*(\.jpg|\.jpeg|\.gif|\.png|\.bmp|\.amerdave\.mlk|\.jpe|\.j1|\.62010|\.sh)\'.*' #'
while read -r line; do
case "$line" in
*+(jpe+(g))*) pattarr=("${jpegs[@]}"); testme=1 ;;
*+(jpg)*) pattarr=("${jpgs[@]}") ;;
*+(jpe!(g))*) pattarr=("${jpes[@]}") ;;
*+(png)*) pattarr=("${pngs[@]}") ;;
*+(mlk|bmp|amerdave)*) pattarr=("${loose[@]}") ;;
*+(j1|62010|sh)*) pattarr=("${odd[@]}") ;;
*) printf -- '%s' "$line" >> "$fixed" && continue ;;
esac
if (( testme )); then
shortline="${line#*2012/05/}"
shortline="${line%jpeg*}"
fi
for f in "${pattarr[@]}"; do
if [[ $line == *${f##*/}* ]]; then
new="$(printf -- '%s' "$line" | sed -e "s:05/${f##*/}:${f}:g")"
break
fi
done
if [[ -n "$new" ]]; then
printf -- '%s\n' "$new" >> "$fixed"
else
printf -- '%s\n' "$line" >> "$fixed"
fi
# done < <(tail -n +"$line_no" "$broken")
done < "$broken"
sed -i 's:\\n:\n:g' "$fixed"
sed -i 's:\\::g' "$fixed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment