<0xEF,0xBB,0xBF> character showing up in files. How to remove them?

FileUnicodeUtf 8Utf

File Problem Overview


I am doing compressing of JavaScript files and the compressor is complaining that my files have  character in them.

How can I search for these characters and remove them?

File Solutions


Solution 1 - File

You can easily remove them using vim, here are the steps:

  1. In your terminal, open the file using vim:

    vim file_name

  2. Remove all BOM characters:

    :set nobomb

  3. Save the file:

    :wq

Solution 2 - File

Another method to remove those characters - using Vim:

> vim -b fileName

Now those "hidden" characters are visible (<feff>) and can be removed.

Solution 3 - File

Thanks for the previous answers, here's a sed(1) variant just in case:

sed '1s/^\xEF\xBB\xBF//'

Solution 4 - File

perl -pi~ -CSD -e 's/^\x{fffe}//' file1.js path/to/file2.js

I would assume the tool will break if you have other utf-8 in your files, but if not, perhaps this workaround can help you. (Untested ...)

Edit: added the -CSD option, as per tchrist's comment.

Solution 5 - File

On Unix/Linux:

sed 's/\xEF\xBB\xBF//' < inputfile > outputfile

On MacOSX

sed $'s/\xEF\xBB\xBF//' < inputfile > outputfile

Notice the $ after sed for mac.

On Windows

There is Super Sed an enhanced version of sed. For Windows this is a standalone .exe, intended for running from the command line.

Solution 6 - File

Using tail might be easier:

tail --bytes=+4 filename > new_filename

Solution 7 - File

@tripleee's solution didn't work for me. But changing the file encoding to ASCII and again to UTF-8 did the trick :-)

Solution 8 - File

I've used vimgrep for this

:vim "[\uFEFF]" *

also normal vim search command

/[\uFEFF]

Solution 9 - File

The 'file' command shows if the BOM is present:

For example: 'file myfile.xml' displays: "XML 1.0 document, UTF-8 Unicode (with BOM) text, with very long lines, with CRLF line terminators"

dos2unix will remove the BOM.

Solution 10 - File

In windows you could use backported recode utility from UnxUtils.

Solution 11 - File

In Sublime Text you can install the Highlighter package and then customize the regular expression in your user settings.

Here I added \uFEFF to the end of the highlighter_regex property.

{
	"highlighter_enabled": true,
	"highlighter_regex": "(\t+ +)|( +\t+)|[\u2026\u2018\u2019\u201c\u201d\u2013\u2014\uFEFF]|[\t ]+$",
	"highlighter_scope_name": "invalid",
	"highlighter_max_file_size": 1048576,
	"highlighter_delay": 3000
}

To overwrite the default package settings place the file here:

> ~/.config/sublime-text-3/Packages/User/highlighter.sublime-settings

Solution 12 - File

I'm suggest the use of "dos2unix" tool, please test to run dos2unix ./thefile.js.

If necessary try to use something like this for multiple files:

for x in $(find . -type f -exec echo {} +); do dos2unix $x ; done

My Regards.

Solution 13 - File

Save the file without code signature.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQuintin ParView Question on Stackoverflow
Solution 1 - FileMohammad AniniView Answer on Stackoverflow
Solution 2 - FileROMANIA_engineerView Answer on Stackoverflow
Solution 3 - FileMichael ShigorinView Answer on Stackoverflow
Solution 4 - FiletripleeeView Answer on Stackoverflow
Solution 5 - FileMasumView Answer on Stackoverflow
Solution 6 - FileDzanvuView Answer on Stackoverflow
Solution 7 - FilePablo TorrecillaView Answer on Stackoverflow
Solution 8 - FileOlexiy ZamkoviyView Answer on Stackoverflow
Solution 9 - FileLittletonDougView Answer on Stackoverflow
Solution 10 - FileNikita KoksharovView Answer on Stackoverflow
Solution 11 - FileJJDView Answer on Stackoverflow
Solution 12 - FileWellington1993View Answer on Stackoverflow
Solution 13 - FileMasood MoshrefView Answer on Stackoverflow