Colorization of Delimited Content with Perl

!!! Jump to “Colorization Script (Perl) for Bar-delimited Content“ for the script.
This post in the series on colorization shows one approach to colorizing, in this case, delimited output. I’d demonstrated it for regular expressions in previous posts (see links below). It’s in Perl and assumes an ANSI terminal of some sort running Bash. That’s the starting point, but it shouldn’t stop you for adapting it as needed. The delimiter here is a bar character, but that’s easy enough for any coder to change.
This is part of a series. You may need these for reference and deeper explanation.
Colorization Script (Perl) for Bar-delimited Content
Place this in a file (e.g. colorize-columns.pl):
#!/usr/bin/perl -w
use strict;
use v5.16.3;
# Simple ANSI color code generation
my @nrm = ( "\e[0m", reverse map { "\e[3" . $_ . "m" } (1..7) );
my @clr = map { ( $_, shift @nrm ) } map { "\e[9" . $_ . "m" } (0..7);
# Arrays used in loop
my (@clrs, @hi, @F);
while (<>) {
@F = split /\|/;
$F[-1] =~ s/\s*[\r\n]+//; # chomp field, O.S. independent
#Colorize
@clrs = (@clrs, @clr) while scalar(@F) > scalar(@clrs);
@hi = map { $_ . (shift @F || "") } @clrs[0..$#F];
say join("\e[90m | ", @hi), "\e[0m";
}
# vi: set ts=8 sw=2:
# vim: set et sta:
This will out-of-the-box colorize bar-delimited content, either from a file argument or stdin (which perlites would expect as de rigor). But, the delimiter can be modified. In fact, notice that the expression for the delimiter is a regular expression (regex). That allows for quiet a wide range of possibilities. Feel free to play with that.
Links (Repeated for Convenience):
Colorization Using Perl with Regular Expressions (includes commentary on coding style and Windows)
Set the permissions (however you like, or…):
chmod ug+x parse-syslog.pl
Tabular Content Generation
!!! Optional
The following is a Bash function to generate bar-delimited content. It’s not required, as you can use any content you want. You can even change the delimiter in the script above.
Paste this in your Bash terminal if you want to generate a table:
genTbl () { (
set +o verbose;
for m1 in $( eval echo "{A..Z}" ); do sep="";
for m2 in $( eval echo "{00..18}" ); do
echo -n "$sep$m1$m2"; sep="|";
done;
echo;
done;
);
}
I had previously had a parameterized version of this that took arguments to var the range of data and the size, but that would require extra explanation. A good Bash hacker can play with that all they want.
Use the above function on the script like this:
genTbl | ./colorize-columns.pl
Obligatory sample output:
Niceties
This is my second post for the day, as I catch up on some old content that I hadn’t gotten online in the past. So I won’t ramble on. I hope you’ve been able to have some fun with this series. There is more to come, particularly for recursion within a regex (which is relatively advanced). Hopefully, I get that out within the week. It’s worth keeping in mind that what I’m posting here is a reference of examples to be adapted. These are not final solutions. And, they are as much a reference for me as for you. I expect that a year from now, I’ll have to come back to this to remember how I did it.
Enjoy
Subscribe to my newsletter
Read articles from TesserId directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
