user1389922
user1389922

Reputation: 51

KSH : How to add BOM to an UTF-8 CSV file

I am creating a shell Script (KSH) and i would like to convert an UTF-8 csv file without BOM to csv file UTF-8 with BOM (or simply add BOM to the the file without BOM).

Thanks for your help

Upvotes: 1

Views: 2350

Answers (1)

Celada
Celada

Reputation: 22261

You could add the aBOMination you want just by outputting it before the contents of the file, so for example to copy from stdin to stdout while adding the aBOMination:

#!/bin/ksh

printf '\357\273\277'
cat

But why would you want to do that? It's almost always a bad idea.

Here's another solution which writes out a new aBOMination only if there is not already one present. As you can see, it's convoluted... which is just one of the reasons why using aBOMinations in the first place is a bad idea. It might require GNU od.

#!/bin/ksh

# write a new aBOMination
printf '\357\273\277'

# now read the first 3 bytes of the original file
set -- $(od -N3 -to1)
if [ "$2" = 357 -a "$3" = 273 -a "$4" = 277 ]; then
    # there already was an aBOMination.
    # Absorb it and write out the rest of the file
    cat
else
    # the first three bytes of the file were normal. Output them.
    if [ $# -lt 3 ]; then
        # file was empty
        :
    elif [ $# -eq 3 ]; then
        # file had only one byte
        printf "\\$2"
    elif [ $# -eq 4 ]; then
        # file had only two bytes
        printf "\\$2\\$3"
    else
        # file had 3 or more bytes.
        # Ouput the first 3
        printf "\\$2\\$3\\$4"
        # then the rest
        cat
    fi
fi

Upvotes: 2

Related Questions