1

I'd like to use awk to parse an output containing color escape codes. But awk fails to interpret colored number as numbers:

echo "[1,2,3]" | jq -C '.[]' | awk '{print $1," -> ",int($1)}'

1  ->  0
2  ->  0
3  ->  0

As you can see the color codes (inserted by jq -C) prevent the string from parsing as an integer. Is there a way to remove/ignore the escape color code from the input ? Bonus point if the solution doesn't use regexes.

gwenzek
  • 2,816
  • 21
  • 21
  • 1
    The easiest solution is to postprocess the `jq` command with `sed "s,\x1B\[[0-9;]*[a-zA-Z],,g"` and then use `awk`. You can also do this in `awk` with `gsub` – kvantour May 07 '20 at 09:11
  • 1
    In Debian there is package `colorized-logs` whis has tool `ansi2txt` which seems to do the job. EDIT: Apparently it's mentioned in the attached link with a fix to bold problem. – James Brown May 07 '20 at 09:44

0 Answers0