Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created March 4, 2014 14:08
Show Gist options
  • Save egulhan/9347093 to your computer and use it in GitHub Desktop.
Save egulhan/9347093 to your computer and use it in GitHub Desktop.
How-to detect file with BOM and remove it
# Source: http://blog.x-if.com/2010/12/find-utf-8-files-with-bom/
# http://stackoverflow.com/questions/204765/elegant-way-to-search-for-utf-8-files-with-bom
find -type f|while read file;do [ "`head -c3 -- "$file"`" == $'xefxbbxbf' ] && echo "found BOM in: $file";done
grep -orHbm1 "^`echo -ne 'xefxbbxbf'`" . | sed '/:0:/!d;s/:0:.*//'
# most efficient way:
find . -type f -print0 | xargs -0r awk '/^xEFxBBxBF/ {print FILENAME} {nextfile}'
# find and remove BOM:
find . -type f -exec sed -i 's/^xEFxBBxBF//' {} ;
# just remove BOM:
tail –bytes=+4 text.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment