Go to file
Radovan Garabík 5bc310369a update credits, remove e-mails 2017-01-27 16:53:52 +01:00
contrib initial commit 2015-01-25 18:36:02 +01:00
debian python3 build dependency 2017-01-27 16:31:15 +01:00
mrsmith initial commit 2015-01-25 18:36:02 +01:00
CHANGES add CHANGES to git 2015-02-27 15:21:56 +01:00
COPYING initial commit 2015-01-25 18:36:02 +01:00
CREDITS update credits, remove e-mails 2017-01-27 16:53:52 +01:00
INSTALL Add homebrew install instructions & fix typo 2016-06-16 18:45:31 -07:00
README.markdown Added 'concat' option for appending matches to a file 2016-09-28 11:48:08 +02:00
Regexp.txt initial commit 2015-01-25 18:36:02 +01:00
TODO initial commit 2015-01-25 18:36:02 +01:00
conf.ant configuratoion for ant and maven 2016-11-25 13:10:44 +01:00
conf.blkid Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.configure More advanced regex for configure colouring 2016-12-27 17:32:48 +00:00
conf.cvs initial commit 2015-01-25 18:36:02 +01:00
conf.df Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.diff Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.dig Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.dnf Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.docker-machinels Fix docker colors, add search, network, version commands 2016-05-14 00:20:54 -05:00
conf.dockerimages resolve conflict 2016-12-03 14:01:24 +01:00
conf.dockerinfo Fix docker colors, add search, network, version commands 2016-05-14 00:20:54 -05:00
conf.dockernetwork Fix docker colors, add search, network, version commands 2016-05-14 00:20:54 -05:00
conf.dockerps Update docker ps and docker-compose ps regex. 2016-11-20 17:13:28 -05:00
conf.dockerpull Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.dockersearch Fix docker colors, add search, network, version commands 2016-05-14 00:20:54 -05:00
conf.dockerversion Fix docker colors, add search, network, version commands 2016-05-14 00:20:54 -05:00
conf.du Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.env Add blkid du env free lsblk lspci commands 2016-05-14 00:33:44 -05:00
conf.esperanto initial commit 2015-01-25 18:36:02 +01:00
conf.free Add blkid du env free lsblk lspci commands 2016-05-14 00:33:44 -05:00
conf.gcc Support more coloring around gcc 2016-12-27 12:27:29 +00:00
conf.getfacl Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.getsebool Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.ifconfig Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.iostat_sar Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.ip unify colours for ip 2016-10-17 21:56:21 +02:00
conf.ipaddr Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.ipneighbor Add ip command and iptables 2016-05-14 00:25:47 -05:00
conf.iproute Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.iptables Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.irclog initial commit 2015-01-25 18:36:02 +01:00
conf.last Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.ldap initial commit 2015-01-25 18:36:02 +01:00
conf.log comment out text replacement 2015-02-27 16:42:33 +01:00
conf.ls Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.lsattr Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.lsblk Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.lsmod Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.lspci Add blkid du env free lsblk lspci commands 2016-05-14 00:33:44 -05:00
conf.mount remove symlink 2017-01-19 10:55:56 +01:00
conf.mount.old Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.mtr initial commit 2015-01-25 18:36:02 +01:00
conf.mvn repair comments 2016-11-29 01:45:49 +01:00
conf.netstat Support matching for IPv6 versions of tcp & udp 2016-12-27 11:37:31 +00:00
conf.nmap Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.php cleanup & updates 2015-02-27 16:34:02 +01:00
conf.ping Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.ping2 alternate ping 2016-07-31 15:34:52 +02:00
conf.proftpd initial commit 2015-01-25 18:36:02 +01:00
conf.ps Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.pv (not working) conf.pv added 2015-11-01 19:40:26 +01:00
conf.semanage Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.showmount Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.sql added config for SQL queries 2016-06-22 23:04:27 +03:00
conf.ss Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.stat Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.sysctl Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.systemctl Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.tcpdump Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.traceroute Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.tune2fs Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.ulimit Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.uptime Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.vmstat Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
conf.wdiff initial commit 2015-01-25 18:36:02 +01:00
grc new version 2017-01-05 16:50:42 +01:00
grc.1 initial commit 2015-01-25 18:36:02 +01:00
grc.bashrc Merge pull request #46 from jlec/bash 2016-12-29 18:16:10 +01:00
grc.conf Merge pull request #44 from jlec/gcc 2016-12-29 18:07:23 +01:00
grc.fish Add several new apps, update grc.fish 2016-06-18 18:18:18 -05:00
grc.spec initial commit 2015-01-25 18:36:02 +01:00
grc.spec.old initial commit 2015-01-25 18:36:02 +01:00
grc.zsh merge 2015-10-10 16:35:08 +02:00
grcat python3 2017-01-05 16:47:47 +01:00
grcat.1 Added 'concat' option for appending matches to a file 2016-09-28 11:48:08 +02:00
install.sh fix installation 2015-11-01 20:03:34 +01:00

README.markdown

Generic Colouriser

Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/software/grc.html garabik @ kassiopeia.juls.savba.sk

For the impatient - try following commands:

grc netstat
grc ping hostname
grc tail /var/log/syslog
grc ps aux

Being overflooded with different logfile colo(u)?ri(s|z)ers, colortails, gccolors, colormakes and similar programs for making text files or outputs of different programs more readable by inserting ansi colour control codes into them, I decided to write my very own colouriser, eventually providing the functions of all those others.

Two programs are provided: grc and grcat. The main is grcat, which acts as a filter, i.e. taking standard input, colourising it and writing to standard output.

Configuration

grcat takes as a parameter the name of configuration file.

Directories ~/.grc/, /usr/local/share/grc/, /usr/share/grc/ are searched for the file (in this order). If the file is not found, it is assumed to be an absolute path of a configuration file located elsewhere.

Configuration file consists of entries, one per regexp, entries are separated with lines with first character non-alphanumeric (except #). Lines beginning with # or empty lines are ignored.

Each entry consists of several lines.

Each line has form: keyword=value

where keyword is one of: regexp, colours, command, concat, skip, replace, count

Only regexp is mandatory, but it does not have much sense by itself unless you specify at least a colour, skip, replace or command keyword as well.

regexp is the regular expression to match.

colours is the list of colours, separated by commas (you can specify only one colour), each colour per one regexp group specified in regexp.

if you use special colour name "previous", colour of the previous line of text will be used (actually, if both the first and last character of the previous line are of different colour than the default one, colour of the first one will be used).

Another special colour name "unchanged" will leave the colour unchanged, useful if you need some context for matching regular expression and you want to leave the colour of context unchanged.

Yet another special name is an arbitrary string enclosed in straight quotes. This string will be inserted directly into the output in front of the matching expression. The string will be eval'ed, so you can use usual python escape sequences.

This is useful on a 256-colour enabled xterm, where e.g. colours="\033[38;5;22m" will give you a dark green (inspired by Rutger Ovidius). Caveat: the string cannot contain a comma. This is due to my laziness :-)

command is command to be executed when regexp matches. Its output will be mixed with normal stdout, use redirectors (>/dev/null) if you want to suppress it.

concat is the name of a file which the current line will be appended to when the regexp matches.

skip can be skip=yes, if that case the matched line is skipped (discarded from the output), or skip=no, when it is not skipped. Default (if you do not have skip keyword) is of course not skipped.

replace means the regular expression match will be replaced by the value. All the preceeding regular expressions will be evaluated against the original text, but if they match and the replacement changes the length of the text, the colouring will stay at the same positions, which is probably not what you want - therefore put the replace rule preferrably at the beginning of config file.

all the following regular expressions will be evaluated against the replaced text, not the original.

replacement is done by re.sub(), therefore you can use anything python supports - in particular, \1, \2 etc... to include text from the original matching groups, e.g:

regexp=(\d\d):(\d\d):(\d\d)
replace=\1h\2m\3s

will change time format from 09:43:59 into 09h43m59s

count is one of words: once, more, stop, previous, block or unblock

  • once means that if the regexp is matched, its first occurrence is coloured and the program will continue with other regexp's.
  • more means that if there are multiple matches of the regexp in one line, all of them will be coloured.
  • stop means that the regexp will be coloured and program will move to the next line (i.e. ignoring other regexp's)
  • previous means the count will be the same as for the previous line
  • block marks a start of a multiline block of text, coloured with the same colour
  • unblock, obviously, marks the end of such a block

example:

# this is probably a pathname
regexp=/[\w/\.]+
colour=green
count=more

this will match /usr/bin, /usr/local/bin/, /etc/init.d/syslogd and similar strings and paint it with green.

Another example:

regexp=^-{1,2}\s{0,1}$
colours=red
count=block
-
regexp=^\s{0,5}$
colours=default
count=unblock

this will turn all correctly formatted mail signatures red.

Regular expressions are evaluated from top to bottom, this allows nested and overlapped expressions. (e.g. you colour everything inside parentheses with one colour, and if a following expression matches the text inside parentheses, it will be also coloured).

Typical usage:

grcat conf.log < /var/log/syslog
/usr/sbin/traceroute www.linux.org | grcat conf.traceroute
grcat conf.esperanto < Fundamento.txt  | less -r

To facilitate the use, command grc acts as frontend for grcat, automatically choosing the configuration files, so you can write:

grc netstat
grc ping hostname
grc tail /var/log/syslog

etc...

grc will execute command command with optional parameters piping its stdout into grcat.

Configuration file for grcat is determined by /etc/grc.conf or ~/.grc/grc.conf file.

Format of /etc/grc.conf or ~/.grc/grc.conf: each entry consists of 2 lines, between entries there can be any number of empty lines or lines beginning with # (comments).

First line is regular expression, second line the name of configuration file for grcat.

Configuration file after the first regular expression matching the rest of line after grc will be passed to grcat as its configuration file

For example, if you have

# log file
\b\w+\b.*log\b
conf.log

# traceroute command
(^|[/\w\.]+/)traceroute\s
conf.traceroute

in your /etc/grc.conf, then typing grc cat /var/log/syslog will use conf.log to colourise the output, grc /usr/sbin/traceroute www.linux.org will use conf.traceroute.

Miscellaneous remarks:

You should get yourself familiar with regular expressions. Good reading is at http://docs.python.org/dev/howto/regex.html

The program is not yet optimized for speed. There are places that can give a big boost if optimized.

Regular expressions are handled by python, it means that they may be slightly different from those you know from perl or grep. It's not my fault in that case.

Colours are one of:

none, default, bold, underline, blink, reverse, concealed,
black, green, yellow, blue, magenta, cyan, white,
on_black, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
beep

on_red means that the background (instead of foreground) is painted with red etc...

Additional colours can be: dark, italic, rapidblink, strikethrough. These are supported only on some terminals, so if you want to write portable configuration files, avoid uing them (idea by James Rowe).

there can be more attributes per line (separated by space), e.g.

# this is probably a pathname
regexp=/[\w/\.]+
colours=bold blink green
count=more

will display pathnames in bold blinking green

Python 3 compatibility

There is some preliminary python3 support, meaning that both grc and grcat will run under both python2 and python3. However, all the regular expressions are strings, and grcat works on string input (not bytes). Therefore it will miserably fail if fed input with invalid UTF-8 characters.

Hints

Following commands will display nice coloured log in virtual console 12:

If you have GNU tail:

tail --follow=name /var/log/syslog | grcat conf.log >/dev/tty12

or, if you have recent BSD tail:

tail -F /var/log/syslog | grcat conf.log >/dev/tty12

If you want to start using grc automatically with supported commands, add

source /etc/grc.bashrc

To set aliases for supported commands, append to your ~/.bashrc: -s "/etc/grc.bashrc" && source /etc/grc.bashrc

Or for zsh users, append to ~/.zshrc: -s "/etc/grc.zsh" && source /etc/grc.zsh

(assuming you copied grc.bashrc and grc.zsh to /etc)