sd
sd copied to clipboard
Capture groups can sometimes come empty
Maybe it's me not using it correctly, but I think I'm correct and therefore while I should capture one or two characters with the capture group, it gives me nothing. See test case below:
Command: sd 'ansible_version.full (.{1,2}) "2.8.0"' "ansible_version.full is version('2.8.0', '$1')" install_dist_pkgs.yml
Source file: https://github.com/newrelic/infrastructure-agent-ansible/blob/master/tasks/install_dist_pkgs.yml
Example of what happens:
- when: nrinfragent_os_name != 'windows' and ansible_version.full >= "2.8.0" and nrinfragent_os_name != 'amazon' and nrinfragent_os_name != 'redhat'
+ when: nrinfragent_os_name != 'windows' and ansible_version.full is version('2.8.0', '') and nrinfragent_os_name != 'amazon' and nrinfragent_os_name != 'redhat'
The comparison symbol should be capture and put where the $1 is, but it always comes up empty. I also tried to use * instead of {1,2}, same result.
Am I wrong in my usage? Is there something tricky/not working as it should?
I just struggled with the same issue and I realized the cause was pretty stupid. So much time wasted. :\
Take this simple example:
> echo test | sd '(.+)' '$1'
test
Look at what happens when you use double quotes instead of single-quotes:
> echo test | sd "(.+)" "$1"
You have to either use single-quotes like in the first example or escape $1 because bash interprets it as a positional parameter:
> echo test | sd "(.+)" "\$1"
test
This should likely be called out clearly in the documentation, but this is in-fact an issue of how the shell is interpreting the args, not an issue with sd