Skip to content

Instantly share code, notes, and snippets.

@anon987654321
Created June 1, 2020 19:27
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 anon987654321/31bce0ff661ed56db5ed159ea69f9cc9 to your computer and use it in GitHub Desktop.
Save anon987654321/31bce0ff661ed56db5ed159ea69f9cc9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
#
# CLEANS UP TEXT FILES
#
# Dependency check
function die {
print >&2 "$@"
exit 1
}
for command in file; do
(( ${+commands[$command]} )) || failed+=($command)
done
if (( $#failed )); then
die "Not found: $failed"
fi
setopt nullglob
setopt extendedglob
for file in **/*; do
# Match text files
if file -b $file | grep -q "text"; then
# Read file into a variable
raw=$(<$file)
# Remove CR+LF
raw=${raw//$'\r'}
# Remove trailing whitespaces
raw=${raw//[[:blank:]]##$'\n'/$'\n'}
# Replace tabs with whitespaces
raw=${raw//$'\t'/ }
# Compress two or more consecutive blank lines
raw=${raw//$'\n'(#c3,)/$'\n\n'}
# Write the variable back into the file
printf "%s\n\n" $raw > $file
echo $file
fi
done
#!/usr/bin/env zsh
#
# CLEANS UP FILE AND FOLDER NAMES
#
setopt nullglob
setopt extendedglob
# Usage
self=$0:t
usage () {
print -- "Usage: $self [-t] [-d] [-f] [-h] [source destination]"
print -- '-t Simulate renames'
print -- '-d Rename folders only'
print -- '-f Rename files only'
print -- '-h'
}
for argument; do
case $argument in
(-t) typeset testing ;;
(-d) typeset do_folders ;;
(-f) typeset do_files ;;
(-h) usage; exit ;;
(*)
if (( $#tr < 2 )); then
tr+=($argument)
else
usage
exit 1
fi
esac
done
(( $+do_files || $+do_folders )) || typeset do_files do_folders
folders () {
# Ignore DVDs
for old in ^(AUDIO_TS|VIDEO_TS); do
if [[ -f $old ]] ; then
if (( $+do_files )); then
file $old
fi
elif [[ -d $old ]]; then
if (( $+do_folders )); then
# Set the new folder and convert to lowercase
new=${(L)old}
if (( $#tr )); then
# Replace source with destination
new=${new//$tr[1]/$tr[2]}
else
# Apply the standard ruleset
new=${new//&/and} # Spell out ampersands
new=${new//\'/} # Neutralize apostrophes
new=${new//[![:alnum:]]##/_} # Replace everything but alphanumerics with underscores
new=${new/%_##/} # Strip trailing underscores
# Nordic characters
new=${new//æ/ae}
new=${new//ø/oe}
new=${new//å/aa}
# Other characters
new=${new//[àáäâ]/a}
new=${new//[èéëê]/e}
new=${new//[ìíïî]/i}
new=${new//[òóöô]/o}
new=${new//[ùúüû]/u}
new=${new//ç/c}
fi
# Rename
if [[ $old != $new ]]; then
if (( ! $+testing )); then
mv -- $old $new
fi
echo "$old => $new"
else
echo "$old: nothing to do"
fi
fi
# Use old name unless it changed
if (( $+testing || ! $+do_folders )); then
new=$old
fi
cd $new
folders
cd ..
else
echo "$new: directory invalid"
fi
done
}
file () {
old=$1
# Set the new filename and convert it to lowercase
new=${(L)old}
# If there's an extension, take it out
if [[ $old == *.* ]]; then
extension=${new##*.}
new=${new%.*}
fi
if (( $#tr )); then
# Replace source with destination
new=${new//$tr[1]/$tr[2]}
else
# Ruleset
new=${new//&/and} # Spell out ampersand
new=${new//\'/} # Neutralize apostrophes
new=${new//[![:alnum:]]##/_} # Replace everything but alphanumerics with underscores
new=${new/%_##/} # Strip trailing underscores
# Nordic characters
new=${new//æ/ae}
new=${new//ø/oe}
new=${new//å/aa}
# Other characters
new=${new//[àáäâ]/a}
new=${new//[èéëê]/e}
new=${new//[ìíïî]/i}
new=${new//[òóöô]/o}
new=${new//[ùúüû]/u}
new=${new//ç/c}
fi
# If there was an extension, put it back in
if [[ $old == *.* ]]; then
new=$new.$extension
fi
# Rename
if [[ $old != $new ]]; then
if (( ! $+testing )); then
mv -- $old $new
fi
echo "$old => $new"
else
echo "$old: nothing to do"
fi
}
folders
#!/usr/bin/env zsh
#
# REPLACES STRINGS IN TEXT FILES
#
# replace <old string> <new string>
#
setopt extendedglob
# Dependency check
function die {
print >&2 "$@"
exit 1
}
for command in file; do
(( ${+commands[$command]} )) || failed+=($command)
done
if (( $#failed )); then
die "Not found: $failed"
fi
echo "Replacing $1 with $2..."
for file in **/*; do
# Match text files
if file -b $file | grep -q "text" && grep -q -- "$1" $file; then
# Sed in-place editing doesn't work the same way on BSD, so use temporary file instead
sed "s/$1/$2/g" $file > $file.tmp
mv -f $file.tmp $file
echo "$file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment