How to insert text before the first line of an UTF-8 with BOM file
0
votes
2
answers
162
views
This question is closely related to: https://unix.stackexchange.com/questions/99350/how-to-insert-text-before-the-first-line-of-a-file . I deliberately made the title similar to that question to highlight this.
Except the target file is UTF-8 with BOM.
So, I want to add a first line to a file that has UTF-8 BOM bytes at its heading (
0xef 0xbb 0xbf
characters). At least the UTF-8 with BOM files I have here begin with this.
If I just go ahead and follow the solutions in the related question,
sed "1i My First line is now this." file.txt
I will get (in VSCode in my case) something like
My First line is now this.
?The first line was this one
Second line and so on
Being ?
in the second line the UTF-8 character to express something non-printable.
Another consequence, as one would expect, is the file no longer opens as UTF-8 with BOM and we rely on the text editor features to "guess" its encoding now. We have determined a pattern in our project to have files with BOM to ensure everything is in the same encoding.
How do I preserve the BOM header in the file while adding the text?
Asked by Avenger
(151 rep)
Dec 6, 2024, 07:19 PM
Last activity: Dec 7, 2024, 12:54 PM
Last activity: Dec 7, 2024, 12:54 PM