Skip to content

sd fails to substitute capture when not surrounded by whitespace #340

Description

@kbd

The title is a guess at a description. Here is current behavior from a script I have that suddenly stopped working, presumably upon my recent Homebrew update. I'm using sd version 1.1.0 according to Homebrew, but it is self-reporting as 1.0.

Context: my script bt gets the list of bluetooth paired devices from blueutil and lets you choose with fzf which one to connect to:

~$ c $(which bt)
     File: /Users/kbd/bin/bt
   1 #!/usr/bin/env bash
   2 set -Eeuo pipefail
   3 
   4 blueutil --paired | sd '.*?address: (.*?),.*name: "(.*?)".*' '$1\x00$2' | fzf0 | xargs blueutil --power=1 --connect

fzf0 is a call to fzf that splits on null, not showing the data before the null in the chooser list, and returns the data. i.e. I don't need to see the mac address in the chooser list, just the name of the device, but blueutil --connect takes the mac address.

$ c $(which fzf0)
     File: /Users/kbd/bin/fzf0
   1 #!/bin/sh
   2 fzf -d'\0' --accept-nth 1 --with-nth 2.. "$@"

Output from blueutil --paired looks like:

address: 2c-76-00-c3-99-2a, not connected, not favourite, paired, name: "KeithPods Pro", recent access date: 2026-04-19 18:12:38 +0000
address: 94-db-56-47-0f-45, not connected, not favourite, paired, name: "WH-1000XM4", recent access date: 2026-04-19 18:12:38 +0000

The problem in the sd call is the '$1\x00$2'. Instead of the second substitution I just get the number 2. This used to work:

$ blueutil --paired | sd '.*?address: (.*?),.*name: "(.*?)".*' '$1\x00$2'
2c-76-00-c3-99-2a2
94-db-56-47-0f-452

If I replace the null character with a space, it works as expected.

$ blueutil --paired | sd '.*?address: (.*?),.*name: "(.*?)".*' '$1 $2'  
2c-76-00-c3-99-2a KeithPods Pro
94-db-56-47-0f-45 WH-1000XM4

To show I'm not crazy, I went back to perl -pe and it works as expected:

$ blueutil --paired | perl -pe 's/.*?address: (.*?),.*name: "(.*?)".*/$1\x00$2/'
2c-76-00-c3-99-2a[null]KeithPods Pro
94-db-56-47-0f-45[null]WH-1000XM4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions