I have some tab-separated file for which I want to conditionally highlight certain columns or certain values.
Example of source file:
wn name Building Name 8 Desc char -> bl
wo bl_id!* Building Code 8 char
I want to:
- put in yellow the contents of the 2nd column when the name is suffixed by "!"
- put in cyan the contents of the 2nd column and in green the contents of the 7th column when there is such a 7th column
- put the little "*" sign in red, in the 2nd column
and a couple of other such rules.
Currently I do that in this way:
cat file.tsv
| sed -r 's/^([^\t]*)\t([^\t]*)!/\1\t\x1b[33m\2!\x1b[0m/' \
| sed -r 's/^^([^\t]*)\t([^\t]*)\t(.*)\t-> ([^\t]+)/\1\t\x1b[36m\2\x1b[0m\t\3\t-> \x1b[32m\4\x1b[0m/' \
| sed -r 's/\*/\x1b[31;1m&\x1b[0m/'
But it's quite complex to read and update.
Is there a better way? I'm quite sure of, but which one?
Are things like GRC or Supercat a way to go? Though, I have to admit that I have a quite important constraint: I'd like the solution to work out-of-the-box in Cygwin. Don't want to have to compile myself tools in there -- for portability reasons of my code.
Can you give hints on how to improve the code to get such a "highlight" functionality?
You could do it with GNU awk, like this (col.awk):
Use it like this
gawk -f col.awk file.tsv