Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am having some issues in using an answer provided on this site for this question about a sed command to replace a blank line with two other lines of content, and it was brought up if the sed command on Mac OS (10.6.7 for me) is different. I don't think that it is, but was wondering if others on this site thought differently.

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

The behavior of shell utilities does differ in minor ways between unix variants. There are many unix variants, with a complex history. There are standardisation efforts such as the POSIX standard and its superset the Single UNIX specification. Most systems nowadays implement POSIX:2001, also known as the Single UNIX Specification version 3, with minor deviations and many extensions. The Single Unix specification is not a tutorial, but version 3 is readable if you already have an idea of what a command is doing. You can consult it to know if some feature is standard or an extension of a particular system.

A majority of unix users use Linux and haven't used any other variant. Linux comes with GNU utilities, which often have many extensions to the standard. So you'll find quite a lot of code out there that works on Linux but not on other unices, because it relies on those extensions.

Regarding sed, consult the sed Single Unix specification for the minimum that every system is supposed to support, the man page on your system for what your implementation supports, and the GNU sed manual for what most people out there use.

One of the nonstandard extensions in GNU sed is supporting multiple commands run together. For example, this GNU sed program prints all lines containing an a, but changes b into c first:

sed -ne '/a/ {s/b/c/g; p}'

{ and } are actually separate commands, so for full portability, you need to specify them either on separate lines (in a file) or in separate -e arguments (on the command line). The lack of a command separator after { and the use of ; as a command separator are common extensions. The lack of a command separator before } is a less common extension. This is standard-compliant:

sed -n -e '/a/ {' -e 's/b/c/g' -e p -e '}'

This is nonstandard but commonly accepted:

sed -ne '/a/ { s/b/c/g; p; }'

Another nonstandard but common extension is the use of \n to mean a newline in a s replacement text (the use in a regexp is standard). The portable method is to include backslash-newline in the sed script. Another common extension is \+, \? and \| in regexps to mean one or more, at most one and alternation; portable basic regular expressions have none of these. For example, the first command is a non-portable way of replacing sequences of whitespace by a newline; the second command is a standard-compliant equivalent.

sed -e 's/ \+/\n/'
sed -e 's/  */\
'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.