I'm working on a commandline that retrieve some data (curl), extract the relevant fields (awk) and format it (column).
It work nice, although it's very ugly (but all my script begin from a "too long and ugly" one-liner) but when I try to have some colour column goes wrong.
This is the plain (reduced) version that work:
curl "http://webservices.rm.ingv.it/fdsnws/event/1/query?lat=42.35&lon=13.4&maxradius=5.0&starttime=2016-01-01T00:00:00&endtime=2016-12-31T23:59:59&minmag=5&format=text&orderby=time-asc" 2>/dev/null \
| awk 'BEGIN { FS= "|"; OFS= "|" } {print $1, $2, $5, $10, $11, $13}' \
| column -t -s '|'
Now, I want to underline some fields, and then add some ANSI escape code in awk:
curl "http://webservices.rm.ingv.it/fdsnws/event/1/query?lat=42.35&lon=13.4&maxradius=5.0&starttime=2016-01-01T00:00:00&endtime=2016-12-31T23:59:59&minmag=5&format=text&orderby=time-asc" 2>/dev/null \
| awk 'BEGIN { FS= "|" ; OFS= "|" } \
$13~/Rieti/||/Perugia/ {$13="\033[1;31m"$13"\033[0m"} \
$11~/[0-9]+/ && $11 > 5.8 {$11="\033[1;33m"$11"\033[0m"}
{print $1, $2, $5, $10, $11, $13 }' \
| column -t -s '|'
Now, the alignment is wrong (see the picture).
Why? And how can I fix it?
UPDATE
I already saw the question Issue with column command and color escape codes but does not solve my problem because his answers are applied and work in the case of a fully colored line.
In my case I can't apply or adapt the answers (or I'm not able to) because:
- The problem is circumscribed to the case where the column $11 is colored, regardless of the subsequent column.
- I can't see a good or elegant way to add color code after column.
If I send column's output to awk for the test I don't know how to instruct awk to separate the fields correctly (if the fields were separated by more space I could use a regex but in some cases the separation is by a single space, and awk would not know how to recognize spaces between words and spaces as Fields Separators).
The only thing I can see is that if I move the reset color code from the assignment to the print block the first row was better spaced, like the plain output version (see below, the \033[0m
underlined in second commandline):
Then, how can we fix it? There is another way, more elegant, to colorize as I did?
(I know, I can do it better with some lines of perl, but I'm curious about this problem)