Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How portable is it to use semi-colons as command separators in sed?

Tags:

sed

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?

like image 400
William Pursell Avatar asked Jun 08 '11 14:06

William Pursell


2 Answers

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:]]*/ :-)

like image 112
Abbafei Avatar answered Oct 05 '22 12:10

Abbafei


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

like image 20
Fredrik Pihl Avatar answered Oct 05 '22 11:10

Fredrik Pihl