13

In sed, it is fairly common to use multiple commands separated by semi-colons:

$ sed -e '/re/{s//replace/p; q;}

However, the standard (eg http://pubs.opengroup.org/onlinepubs/9699919799/ ) only allows for newlines as a separator:

$ sed -e '/re/{
    s//replace/p
    q
}

Are there many common implementations of sed still in use that do not allow the semi-colon? IOW, can a sed script intended to be portable use semi-colons?

William Pursell
  • 204,365
  • 48
  • 270
  • 300

3 Answers3

9

From the POSIX specification of sed :

Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by a <semicolon>, optional <blank> characters, and another command verb. However, when the s command verb is used with the w flag, following it with another command in this manner produces undefined results.

So most commands, besides those mentioned above, can be separated by a semicolon.

/[^\{abcirtw:#];[[:space:]]*/ :-)

Abbafei
  • 3,088
  • 3
  • 27
  • 24
3

Tricky one... Only reference I could find about this is in the sed-faq chapter 6.8.1

Most versions of sed permit multiple commands to issued on the command line, separated by a semicolon (;).

The only reference towards ; not working is for HHSED, see Chapter 7

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 1
    Weirdly, the command separator isn't documented in sed's (at least GNU) documentation (and no, a FAQ I consider *extra* material, you shouldn't need it to get the full info). – Jürgen A. Erhard Mar 01 '13 at 22:16
1

GNU sed seems to allow semi-colons in a few places that MacOS X (BSD) sed does not. I don't have the details at my fingertips, now, but several times I've had to modify answers to get them to work properly on Mac. The issue may have been brace-enclosed command sequences.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • At a mimimum, BSD sed appears to insist upon a final semicolon before closing curly braces where GNU sed does not. GNU: `{n;p}` BSD: `{n;p;}` – stevesliva Aug 15 '17 at 13:24