The application here is "sanitizing" strings for inclusion in a log file. For the sake of argument, let's assume that 1) colorizing the string at runtime is proper; and 2) I need leading and trailing spaces on screen but excess whitespace removed from the log.
The specific application here is tee-ing into a log file. Not all lines would be colorized, and not all lines would have leading/trailing spaces.
Given this, I want to
- Remove all codes both setting the color and resetting. The reason for this will be apparent in a moment
- Remove leading and trailing whitespace
When you search (anywhere) for how to strip color codes in bash, you can find many different ways to accomplish it. What I have discovered so far however is nobody seems to address the trailing reset; the $(tput sgr0). In the examples I have seen this is inconsequential, however my additional requirement to strip leading/trailing spaces complicates it/makes it a requirement.
Here is my example script which demonstrates the issue:
#!/bin/bash
# Create a string with color, leading spaces, trailing spaces, and a reset
REPLY="$(tput setaf 2) This is green $(tput sgr0)"
echo "Colored output: $REPLY"
# Remove initial color code
REPLY="$(echo "$REPLY" | sed 's,\x1B\[[0-9;]*[a-zA-Z],,g')"
echo "De-colorized output: $REPLY"
# Remove leading and trailing spaces if present
REPLY="$(printf "%s" "${REPLY#"${REPLY%%[![:space:]]*}"}" | sed -n -e 'l')"
echo "Leading spaces removed: $REPLY"
REPLY="$(printf "%s" "${REPLY%"${REPLY##*[![:space:]]}"}" | sed -n -e 'l')"
echo "Trailing spaces removed: $REPLY"
The output is (can't figure out how to color text here, assume the first line is green, subsequent lines are not):
I am willing to see the error of my ways, but after about three hours trying different things, I'm pretty sure my google-fu is failing me.
Thanks for any assistance.