Remove multiple BOMs from a file

后端 未结 5 870
执念已碎
执念已碎 2020-12-15 10:04

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 us

相关标签:
5条回答
  • 2020-12-15 10:08

    I have written a bash script see here that works for Mac, I haven't tested on other systems but I suspect it should work there as well. The script also support files or file paths that contains spaces.

    Examples

    Remove BOM from all files in current directory:

    rmbom .

    Print all files with a BOM in the current directory

    rmbom . -a

    Only remove BOM from all files in current directory with extension txt or cs:

    rmbom . -e txt -e cs

    Print help

    rmbom -h

    0 讨论(0)
  • 2020-12-15 10:10

    See also: Using awk to remove the Byte-order mark

    To remove multiple BOMs from anywhere within a text file you can try something similar. Just leave out the ^ anchor:

    perl -e 's/\xef\xbb\xbf//;' -pi~ file.js
    

    (This edits the file in-place. But creates a backup file.js~.)

    0 讨论(0)
  • 2020-12-15 10:12

    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#
    0 讨论(0)
  • 2020-12-15 10:18

    I also figured out this solution which works entirely in PHP:

    $packed = pack("CCC",0xef,0xbb,0xbf);
    $contents = preg_replace('/'.$packed.'/','',$contents);
    
    0 讨论(0)
  • 2020-12-15 10:19

    I normally do it using vim:

    vim -c "set nobomb" -c wq! myfile
    
    0 讨论(0)
提交回复
热议问题