Skip to main content
Commonmark migration
Source Link

In my case, I wanted to selectively colorise values in a column depending on its value. Let's say I want okokokok to be green and blabla to be red.

I can do it such way (the idea is to colorise values of columns after columnisation):

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color

column -s$'\t' -t <original file> | echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g")"

Alternatively, with a variable:

DATA=$(column -s$'\t' -t <original file>)

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color
echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g" <<< "$DATA")"

Take a note of that additional backslash in values of color definitions. It is made for sed to not interpret an origingal backsash.

This is a result:

[![enter image description here][1]][1] [1]: https://i.sstatic.net/iGoNt.pngenter image description here

In my case, I wanted to selectively colorise values in a column depending on its value. Let's say I want okokokok to be green and blabla to be red.

I can do it such way (the idea is to colorise values of columns after columnisation):

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color

column -s$'\t' -t <original file> | echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g")"

Alternatively, with a variable:

DATA=$(column -s$'\t' -t <original file>)

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color
echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g" <<< "$DATA")"

Take a note of that additional backslash in values of color definitions. It is made for sed to not interpret an origingal backsash.

This is a result:

[![enter image description here][1]][1] [1]: https://i.sstatic.net/iGoNt.png

In my case, I wanted to selectively colorise values in a column depending on its value. Let's say I want okokokok to be green and blabla to be red.

I can do it such way (the idea is to colorise values of columns after columnisation):

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color

column -s$'\t' -t <original file> | echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g")"

Alternatively, with a variable:

DATA=$(column -s$'\t' -t <original file>)

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color
echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g" <<< "$DATA")"

Take a note of that additional backslash in values of color definitions. It is made for sed to not interpret an origingal backsash.

This is a result:

enter image description here

Source Link
Ashark
  • 821
  • 8
  • 20

In my case, I wanted to selectively colorise values in a column depending on its value. Let's say I want okokokok to be green and blabla to be red.

I can do it such way (the idea is to colorise values of columns after columnisation):

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color

column -s$'\t' -t <original file> | echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g")"

Alternatively, with a variable:

DATA=$(column -s$'\t' -t <original file>)

GREEN_SED='\\033[0;32m'
RED_SED='\\033[0;31m'
NC_SED='\\033[0m' # No Color
echo -e "$(sed -e "s/okokokok/${GREEN_SED}okokokok${NC_SED}/g" -e "s/blabla/${RED_SED}blabla${NC_SED}/g" <<< "$DATA")"

Take a note of that additional backslash in values of color definitions. It is made for sed to not interpret an origingal backsash.

This is a result:

[![enter image description here][1]][1] [1]: https://i.sstatic.net/iGoNt.png