Friday 4 February 2022

How to remove BOM Unicode character from a file (Linux)

Example of file with Byte Order Mark (BOM) bytes which appear at the beginning of the file:
 
$ xxd commands.sql
00000000: efbb bf73 656c 6563 7420 5573 6572 2066  ...select User f
00000010: 726f 6d20 6d79 7371 6c2e 7573 6572 3b    rom mysql.user;

To remove these bytes, we can use sed tool:

$ sed -i '1s/^\xef\xbb\xbf//' commands.sql 

Let's check now the file content:
 
$ xxd commands.sql
00000000: 7365 6c65 6374 2055 7365 7220 6672 6f6d  select User from
00000010: 206d 7973 716c 2e75 7365 723b             mysql.user;

No comments: