Search code examples
regexgitbashsedps1

How do I remove this space in this sed regex?


I have a script from a coworker that formats my prompt. It grabs my branch and formats with the code below using sed (note I hardcoded the branch name for testing):

echo "* master" | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1 /'

In an effort to learn more of what the script was doing, I was playing around with it. I noticed it prints the name of my branch "master" but it leaves the space before the branch name (" master"). I want to eliminate the space. I can't seem to do this though.


Solution

  • Just remove the space before your \1. Probably you want to remove the space behind it as well.

    echo "* master" | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'