Skip to content

Instantly share code, notes, and snippets.

@dmikushin
Created January 15, 2025 08:36
Show Gist options
  • Save dmikushin/4d1119c8182662a48e30da90aec56816 to your computer and use it in GitHub Desktop.
Save dmikushin/4d1119c8182662a48e30da90aec56816 to your computer and use it in GitHub Desktop.
Fix source file character encoding
#!/bin/bash
set -e
set -x
# Check if file name is provided
if [ -z "$1" ]
then
echo "No file name provided"
exit 1
fi
# Determine the file encoding
encoding=$(file -bi "$1" | sed -e 's/.*[ ]charset=//')
if [ "$encoding" = "unknown-8bit" ]
then
encoding="ISO-8859-1"
fi
# Convert the file to UTF-8
if [ "$encoding" != "utf-8" ]
then
iconv -f "$encoding" -t utf-8 "$1" > "$1".utf8
mv "$1".utf8 "$1"
echo "File converted to UTF-8"
else
echo "File is already UTF-8"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment