Running bash color codes through sed

Running bash color codes through sed
Photo by Robert Katzki / Unsplash

I have a script that runs a search and replace on a git status. I recently found out how to do git config color.status always, so that git status gives color codes to my functions. However, this broke the substitution, and it took me a while to figure it out.

At the end of every line, there was an invisible 'reset' color code, and this meant that sed "s/string$/$replace" was no longer finding string$.

Here is an example of how to use sed when your string contains color codes:

# Need to strip the color character from the end of the line, otherwise
# EOL '$' doesn't work. This gave me a headache for long time.
# The echo ~> regex is time-consuming, so perform a simple search first.

if [[$line == *$search*]]; then
  line=$(echo $line | sed -r "s:$search(\x1B\[m)?$:$replace:g")

You might also find this page useful: