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:
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):
function colText ( text, col) {
return sprintf("\033[%sm%s\033[39;0m", col, text);
}
function yellow( text ){ return colText( text, "33;1" ); }
function cyan ( text ){ return colText( text, "36;1" ); }
function green ( text ){ return colText( text, "32;1" ); }
function red ( text ){ return colText( text, "31;1" ); }
BEGIN {FS=OFS="\t";}
# red * in $2
$2 ~ /\*/ {gsub(/\*/, red("*"), $2); }
# cyan if col 7 present
NF == 7 {print $1, cyan($2), $3, $4, $5, $6, green( $7 ) ;
next;}
# yellow col2 if there is a !
$2 ~ /!/ {print $1, yellow($2), $3, $4, $5, $6, $7 }
Use it like this gawk -f col.awk file.tsv