Score:0

How to save input colors in awk?

al flag

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?

HBruijn avatar
in flag
*"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.
Score:3
br flag

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}'
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.