Skip to content

Instantly share code, notes, and snippets.

@sportebois
Last active January 27, 2021 13:54
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 sportebois/db81d74a47fefd78aef38ac65d2c076f to your computer and use it in GitHub Desktop.
Save sportebois/db81d74a47fefd78aef38ac65d2c076f to your computer and use it in GitHub Desktop.
Remove BOM from UTF8 files
#!/usr/bin/env bash
set -eu
# find-bom searches for nonbinary files starting with a BOM marker
# Path of BOM-encoded files is print to stdout
# Exit status is 0 if no BOM files has been found
# Exit status is 1 if at least one BOM file has been found
readonly TARGET_ROOT=${1:-$PWD}
echo "Searching for BOM in $TARGET_ROOT"
bomFiles=$(grep -rlI $'\xEF\xBB\xBF' "$TARGET_ROOT" --exclude-dir={.tox,.git,.venv,.hypothesis,.mypy_cache} || echo "")
if [ -z "$bomFiles" ]; then
exit 0
fi
for f in $bomFiles; do
echo "$f"
done
exit 1
#!/usr/bin/env bash
set -eu
# fix-bom search for BOM marker in non-binary files and removes them
# You can either call the script as-is, to run it in the current working folder, and all its children recursively
# Or you can pass a target folder as the command line argument
#
# Sample use
# ./fix-bom.sh
# ./fix-bom.sh ./only/this/specific/tree/
readonly TARGET_ROOT=${1:-$PWD}
echo "Searching for BOM in $TARGET_ROOT"
bomFiles=$(grep -rlI $'\xEF\xBB\xBF' "$TARGET_ROOT" --exclude-dir={.tox,.git,.venv,.hypothesis,.mypy_cache} || echo "")
for f in $bomFiles; do
echo "Removing BOM from $f"
sed -i '1s/^\xEF\xBB\xBF//' "$f"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment