Skip to content

Instantly share code, notes, and snippets.

@jmatsu
Last active August 29, 2015 14:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmatsu/26a9a997d4d5af7c5e97 to your computer and use it in GitHub Desktop.
Save jmatsu/26a9a997d4d5af7c5e97 to your computer and use it in GitHub Desktop.
Converts report files, which are non-text format although "txt", into text format or into correct format. For MacOSX.
#!/usr/bin/env bash
set -ue
function judge(){ echo "$1" | grep -e "$2" &> /dev/null; return $?; }
function judge_ms(){ unzip -Z "$1" | grep "$2" &> /dev/null; return $?; }
_FUCK_U=""
for txt in `find . -type f -iname \*.txt`; do
_TYPE=`nkf -g $txt`
case $_TYPE in
"BINARY") _FUCK_U="${_FUCK_U} ${txt}" ;;
"ASCII") _FUCK_U="${_FUCK_U} ${txt}" ;;
esac
done
for fuck in $_FUCK_U; do
_MIME=`file -I "$fuck" | awk '{print $2}'`
{ judge "${_MIME}" "text/rtf" &&
cp "$fuck" "${fuck}.rtf"
} || { judge "${_MIME}" "application/pdf" &&
cp "$fuck" "${fuck}.pdf"
} || { judge "${_MIME}" "application/vnd.ms-excel" &&
echo "Fuck'n VERY KIND Microsoft!"
cp "$fuck" "${fuck}.xls"
} || { judge "${_MIME}" "application/vnd.ms-word" &&
echo "Fuck'n VERY KIND Microsoft!"
cp "$fuck" "${fuck}.doc"
} || { judge "${_MIME}" "application/zip" && {
echo "Fuck'n VERY KIND Microsoft!"
{ judge_ms "${fuck}" "word" &&
cp "$fuck" "${fuck}.docx"
} || { judge_ms "${fuck}" "excel" &&
cp "$fuck" "${fuck}.xlsx"
}
} }
mv "$fuck" "${fuck}.backup"
done
for rtf in `find . -type f -iname \*.rtf`; do
textutil -convert txt $rtf
done
for src in `find -E . -type f -iregex '^.*\.(txt|c|java)$'`; do
nkf -w --overwrite $src
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment