sed behaves different on FreeBSD and on Linux?

Maybe you should use the -E option(or -r as explained on the manual) to keep compatibility with GNU Sed. In your case, you could install Gnu Sed if you are used to it (port gsed on FreeBSD), or it will take a long effort to port scripts.

And remember. If some command on BSD does not act like the gnu version of that utility, it doesn´t mean it´s broken ;)


Yes, there are various differences, the behavior of -i being the only one I know of off the top of my head.

I've never used BSD so I can't really help with the details but a workaround might be to use tr instead:

tr '\t' , < inputFile.txt > outputFile.csv

A pleasant side effect is that tr should be significantly faster. I tested that on my Linux using a test file with 50000 lines, each of which had 2 tabs:

$ time tr '\t' , < foo.txt > /dev/null 

real    0m0.004s
user    0m0.000s
sys     0m0.000s

$ time sed 's/\t/,/g' foo.txt > /dev/null 

real    0m0.039s
user    0m0.036s
sys     0m0.000s

Yes, unlike GNU sed FreeBSD sed does not interpret ANSI C escape sequences such as \t in regular expressions.

One way to get a least common denomiator in this case is to use printf.

tab="$(printf '\t')"
printf '\t\n' | sed 's/'"${tab}"'/,/g'
printf '\t\n' | sed 's/'"$(printf '\t')"'/,/g'

The behaviour of sed -i for in-place file edits can be made compatible if a switch or option immediately follows the -i switch, e. g. sed -i -e 's/x/X/g' file works for both GNU sed as well as FreeBSD sed.

Recent versions of FreeBSD sed (FreeBSD 8.1 or newer) have the -r switch to increase compatibility with GNU sed.

(In addition, the use of POSIX character classes in sed regular expressions is a good way to ensure compatibility as well).

For an alternative, POSIX-conformant sed implementation see: minised - a smaller, cheaper, faster SED implementation.

Tags:

Sed

Freebsd