Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove multiple BOMs from a file

I am using a Javascript file that is a concatenation of other JavaScript files.

Unfortunately, the person who concatenated these JavaScript files together did not use the proper encoding when reading the file, and allowed a BOM for every single JavaScript file to get written to the concatenated JavaScript file.

Does anyone know a simple way to search through the concatenated file and remove any/all BOM markers?

Using PHP or a bash script for Mac OSX would be great.

like image 758
Macy Abbey Avatar asked Feb 01 '12 17:02

Macy Abbey


People also ask

How do I remove byte order mark?

How to remove BOM. If you want to remove the byte order mark from a source code, you need a text editor that offers the option of saving the mark. You read the file with the BOM into the software, then save it again without the BOM and thereby convert the coding. The mark should then no longer appear.

How do I remove a BOM character from a csv file in Python?

Set the encoding to utf-8-sig to remove the BOM character when reading from a file, e.g. with open('example. txt', 'r', encoding='utf-8-sig') as f: . The utf-8--sig encoding skips the BOM byte if it appears as the first byte in the file.

What is BOM in XML file?

The Byte-Order-Mark (or BOM), is a special marker added at the very beginning of an Unicode file encoded in UTF-8, UTF-16 or UTF-32. It is used to indicate whether the file uses the big-endian or little-endian byte order. The BOM is mandatory for UTF-16 and UTF-32, but it is optional for UTF-8.


2 Answers

fetch BOM files

grep -rIlo $’^\xEF\xBB\xBF’ ./

remove BOM files

grep -rIlo $’^\xEF\xBB\xBF’ . | xargs sed –in-place -e ‘s/\xef\xbb\xbf//’

exclude .svn dir

grep -rIlo –exclude-dir=”.svn” $’^\xEF\xBB\xBF’ . | xargs sed –in-place -e ‘s/\xef\xbb\xbf//’

  • See more at: http://www.a5go.com/how-to-remove-bom-from-utf-8-using-sed.html#
like image 175
user1896665 Avatar answered Sep 18 '22 15:09

user1896665


I normally do it using vim:

vim -c "set nobomb" -c wq! myfile
like image 20
Pål Brattberg Avatar answered Sep 21 '22 15:09

Pål Brattberg