How would an awk
script (presumably a one-liner) for removing a BOM look like?
Specification:
NR > 1
)#FE #FF
or #FF #FE
, remove those and print the restIf 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.
Using GNU sed
(on Linux or Cygwin):
# Removing BOM from all text files in current directory: sed -i '1 s/^\xef\xbb\xbf//' *.txt
On FreeBSD:
sed -i .bak '1 s/^\xef\xbb\xbf//' *.txt
Advantage of using GNU or FreeBSD sed
: the -i
parameter means "in place", and will update files without the need for redirections or weird tricks.
On Mac:
This awk
solution in another answer works, but the sed
command above does not work. At least on Mac (Sierra) sed
documentation does not mention supporting hexadecimal escaping ala \xef
.
A similar trick can be achieved with any program by piping to the sponge
tool from moreutils:
awk '…' INFILE | sponge INFILE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With