I'm on OS X, which has a BSD version of sed (which I'm guessing is inferior to a GNU version on Linux), and all the techniques for removing color on commandlinefu.com did not work. I tried replacing the -r switch (non-existent on OS X) with -e, but that didn't help.

Is there some reliable way I can remove color formatting codes on OS X? Here's the command I'm running that needs color output stripped:

for concurrency in $(seq 1 50); do siege -f urls.txt -c $concurrency -t 5m >> results.csv ; done

and here's an example of trying to use sed to strip the color from the CSV output:

for concurrency in $(seq 1 50); do siege -f urls.txt -c $concurrency -t 5m | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" >> results.csv ; done

With bash, this ought to pass sed appropriate arguments, I think:

sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g'

Or, more portably to other shells, you could try:

sed "s,$(printf '\033')\\[[0-9;]*[a-zA-Z],,g"

Or just putting an escape byte directly into the string by typing Ctrl-vEsc instead of typing \x1b.

Note, though, that this sed statement removes more than just color codes, potentially - it could match many control sequences starting with a CSI. Search in page for "CSI" in this to get an idea of what other control sequences it would remove (or possibly mangle).

  • 1
    The bash version worked without any adjustment needed: you deserve more upvotes than all the people on commandlinefu.com! – iconoclast Jul 2 '14 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.