BSD sed in Mac is … funny

P.P.S. (2013 Oct. 22)

I felt somewhat embarrassed when I found not only grep but also sed has option -E for extended RE contrary to option -e for basic RE. Whole point of thinking sed funny will be meaningless when sed eventually has a modern capability to handle extended RE!

Mac OS X comes with BSD sed, but it's not useful. If you are planning to use sed for casual purposes (not within some build systems like Xcode or Make), I recommend using GNU sed instead; you can get it using package managers like MacPorts and Homebrew.

I had a situation the preinstalled sed did not work. The command I tried that time is as follows:
sw_vers -productVersion | sed -e "s/^\(10\.[0-9]\+\)\.[0-9]\+/\1/"
It does not work; if sw_vers part passes "10.8.5", sed part should output "10.8" . It seems the preinstall sed chokes on regular expression [0-9]\+ and GNU sed doesn't.

My plan for using sed inside Makefile for pattern match of Mac OS X version should use other means. Anyway, I wanted regular expression ^(10\.[\d]+)(^?\.[\d]+)? to work, but sadly it does not in both BSD and GNU seds.

But yesterday, I found a Makefile using built-in function $(filter ,) with sw_vers -productVersion. Its pattern match capability is naive and not full-fledged, but it works. Here is an example:
MAC_OS_VER=$(shell sw_vers -productVersion)
ifneq ($(filter 10.6 10.6.% 10.7 10.7.% 10.8 10.8.%,$(MAC_OS_VER)),)


With some googling and reading of man pages, I decided to use grep -E (not egrep) for pattern match. Man page of grep I read said that -E option is standardized in POSIX, it means grep -E is widely available to POSIX systems, thus is seemingly qualified enough to be used in Makefile.

I can use grep -E in conditionals in Makefile. If it is non-empty, it means a regular expression pattern is found in input, otherwise not.

echo "10.8" | grep -E "^10\.[4-8]+(\.[0-9]+)?"
# Match (output: "10.8")

echo "10.8.5" | grep -E "^10\.[4-8]+(\.[0-9]+)?"
# Match (output: "10.8.5")

echo "10.9" | grep -E "^10\.[4-8]+(\.[0-9]+)?"
# No match (output: "")


Popular posts from this blog

Hardcoding Data Like Images and Sounds into HTML or CSS

DDS for GIMP (Mountain Lion, Native, no X11 GUI)

Make It Work: Global .gitattributes