0

Given the following string:

"foo\e[38;5;1mbar\e[0mbaz"

How can I remove both \e[38;5;19m and \e[0m with sed without knowing the exact values of the numbers within those strings? Expected output:

foobarbaz

I have the following sed line, which matches the first string:

# regex: \\e\[[^/]*m
sed -E 's/\\e\[[^/]*m//g'

This returns sed: 1: unbalanced brackets ([]).
How can I get rid of both strings where the numbers are not known?

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
k-a-v
  • 326
  • 5
  • 22

1 Answers1

2

You were very close. This should do the job:

$ echo "foo\e[38;5;1mbar\e[0mbaz" | sed -E 's/\\e[^\\]*m//g'
# foobarbaz
Paolo
  • 21,270
  • 6
  • 38
  • 69