On Thu, Mar 27, 2025 at 1:14 AM Jeff King <peff@xxxxxxxx> wrote: > On Wed, Mar 26, 2025 at 02:07:10PM -0400, Eric Sunshine wrote: > > It's probably an indication that I've done too much `sed` programming, > > but I find Dscho's version more obvious. That aside, your response > > made me take a closer look at what Dscho wrote and I noticed that it > > is syntactically flawed, at least for BSD-lineage `sed`. Testing on > > macOS reveals that this is indeed so: > > > > % LANG=C LC_ALL=C cc -v 2>&1 | sed -n '/ version /{p;q}' > > sed: 1: "/ version /{p;q}": extra characters at the end of q command > > > > The problem is that the `q` function takes no arguments, but > > BSD-lineage `sed` thinks that the closing `}` is an argument rather > > than a terminator. Fixing this requires inserting a terminator after > > `q`, which will be either a newline character or a semicolon. So, the > > correct form is: > > > > sed -n '/ version /{p;q;} > > Heh, I think it was the braces and semicolons that made my spider-sense > tingle, probably because I've been bitten by those subtleties in the > past. > > I think just "/foo/p;q" works on GNU sed, but no idea if it does > elsewhere. What you wrote seems the safest. That's not quite the same, though. The patternless `q` will cause `sed` to terminate upon reading the first line of input, not upon the first line which contains " version ". This matters, for instance, if the first line output by `$CC -v` is not the version string (i.e. it might be a copyright notice).