0

I've seen many more or less related topics, but all I found discuss how to color awk output. I need to preserve the color that was provided as awk input. For example, now I have:

./some_command
(red_text) (blue_text) (green_text)

./some_command | awk -F' ' '{print $1}'
(red_text) # < but the color is standard terminal color, not red anymore

How to keep the original coloring in the awk output?

Putnik
  • 2,217
  • 4
  • 27
  • 43
  • 1
    *"The reason is not that awk strips colours but rather that "well-designed" programs that produce coloured output typically do so only when their output goes to a terminal, not when it's sent to a pipe or to a regular file. The reason is that data sent on a terminal is presumably read by a human, whereas data piped to a program or written to a file is likely to be parsed by some program, so it shouldn't contain extraneous content like colour-changing escape sequences."* https://unix.stackexchange.com/a/185611/48232 - so you may need to force the original program to generate coloured output. – HBruijn Jul 23 '23 at 12:36

1 Answers1

3

some_command may have an option to force color even when it's connected to a terminal, for example with grep it would be --color=always. If it doesn't though you have to trick some_command into thinking it's connected to a terminal. Using script for example:

script --return --quiet -c 'echo a b | grep --color a'   /dev/null | awk '{print $1}'

There would be no color if you ran grep without script:

echo a b | grep --color a  | awk '{print $1}'