Subject: Re: [PATCH] t9001: avoid not portable '\n' with sed

From: John Keeping <[email-masked]>
Date: Wed, 4 Jun 2014 19:46:04 +0100

On Wed, Jun 04, 2014 at 10:42:46AM -0700, Junio C Hamano wrote:
> Torsten Bögershausen <[email-masked]> writes:
> 
> > t9001 used a '\n' in a sed expression to split one line into two lines.
> > Some versions of sed simply ignore the '\' before the 'n', treating
> > '\n' as 'n'.
> >
> > As the test already requires perl as a prerequisite, use perl instead of sed.
> >
> > Signed-off-by: Torsten Bögershausen <[email-masked]>
> > ---
> 
> Hmph.  I read this in pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
> 
>     The escape sequence '\n' shall match a <newline> embedded in the
>     pattern space.
> 
> so it may be better to be a bit more explicit in the log message to
> say whose implementation has this issue to warn people.
> 
> > -	sed "s/^From:/$header: [email-masked]\nFrom:/" cover-to-edit.patch >"$cover" &&
> > +	"$PERL_PATH" -pe "s/^From:/$header: extra\@address.com\nFrom:/" cover-to-edit.patch | tr Q "$LF" >"$cover" &&

Note that quoted section of POSIX says "embedded in the pattern space";
under the description of the "s" command, it says:

	The replacement string shall be scanned from beginning to end.
	[...]
	The meaning of a <backslash> immediately followed by any
	character other than '&', <backslash>, a digit, or the delimiter
	character used for this command, is unspecified.

	A line can be split by substituting a <newline> into it. The
	application shall escape the <newline> in the replacement by
	preceding it by a <backslash>.

So the portable way to do it is:

	sed "s/^From:/$header: [email-masked]\
From:/" cover-to-edit.patch >"$cover" &&

but that requires the continuation to start in column 0, so the Perl
variant is probably neater.