This little tool will colorize its standard input with a different color for each one of its arguments.
Usage
xcol imitates the usage of grep, so you can pipe any stdout to it
lspci | xcol audio vga pci usb amd ati hdmi ethernet radeon amd intel
, or read from a file
xcol fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge cmov pat pse36 clflush mmx fxsr sse \ sse2 ht syscall nx mmxext cache cores amd /proc/cpuinfo
Just like grep .
You can match any regular expression that sed would accept
sudo netstat -putan | xcol httpd sshd dnsmasq pulseaudio conky tor Telegram firefox \ "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" ":[[:digit:]]+" "tcp." "udp." \ LISTEN ESTABLISHED TIME_WAIT
I recommend it also for things like monitoring logs. Try something like
tail -f /var/log/somelog.log | xcol error warning info denied filtered
Installation
Get the code from github, and append it to your .bashrc or .zshrc.
To do it in one step, paste the following in your zsh terminal
wget https://raw.githubusercontent.com/nachoparker/xcol/master/xcol.sh -O - >> ~/.zshrc
, or in bash
wget https://raw.githubusercontent.com/nachoparker/xcol/xcol_bash/xcol.sh -O - >> ~/.bashrc
the xcol command will be available next time you open a new terminal.
Details
In my daily work, I have always made heavy use of grep –colour as a simple way of highlighting whatever I am interested in on a piece of text. I use so much that I aliased it as alias grep=’grep -i –colour’ .
When you use grep like this, often you lose information of the context around whatever you want to highlight. Sure, you can use grep -C2 or grep -z , but it is more cumbersome to use and still not that great.
While trying to find my way around this, I came across this wonderful post by Andreas Schamanek. He had put the effort into exactly what I was too lazy to do myself. Awesome!
So I tweaked it a little bit to run in zsh, and created a wrapper around it so you do not have to decide what color goes for each match, which in my case comprises 99% of the use cases. I gave it a grep like invocation flavour.
And so, xcol came to life
# Colorize your standard output using xcolorize with a grep-like usage # # Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> # GPL licensed (see end of file) * Use at your own risk! # # Usage piping from stdin: # mount | xcol mnt "sda." "sdb." cgroup tmpfs proc # # Usage reading from a file: # xcol pae fpu vme mhz sse2 cache cores /proc/cpuinfo # # Notes: # It supports sed compatible regular expressions function xcol() { local bold=$(tput bold) # make colors bold/bright local red="$bold$(tput setaf 1)" # bright red text local green=$(tput setaf 2) # dim green text local fawn=$(tput setaf 3); beige="$fawn" # dark yellow text local yellow="$bold$fawn" # bright yellow text local darkblue=$(tput setaf 4) # dim blue text local blue="$bold$darkblue" # bright blue text local purple=$(tput setaf 5); magenta="$purple" # magenta text local pink="$bold$purple" # bright magenta text local darkcyan=$(tput setaf 6) # dim cyan text local cyan="$bold$darkcyan" # bright cyan text local gray=$(tput setaf 7) # dim white text local darkgray="$bold"$(tput setaf 0) # bold black = dark gray text local white="$bold$gray" # bright white text local COLS=( white yellow red cyan gray purple pink fawn ) [ -t 0 ] && local STDIN=0 || local STDIN=1 if [[ $STDIN == 0 ]]; then local ARGVS=${@: 1 : $#-1 } # all arguments except last one local FILE=${@: -1} # last argument is the file name else local ARGVS=$@; fi local IDX=1 # rotate colors in a cycle for arg in ${ARGVS[@]}; do local ARGS=( ${ARGS[@]} ${COLS[$IDX]} $arg ) IDX=$(( IDX + 1 )) [[ $IDX == ${#COLS[@]} ]] && IDX=1 done [[ $STDIN == 1 ]] && { xcolorize --unbuffered ${ARGS[@]} } || { cat $FILE | xcolorize --unbuffered ${ARGS[@]} } } # License # # This script is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this script; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA
Thank you very much for your effort and for sharing your work!
Simple, Good & Useful !
Best. Thing. Ever.
Hey, just a quick question, I’m trying to run xcol on OSX Mojave and I get this error:
sed: illegal option — u
usage: sed script [-Ealn] [-i extension] [file …]
sed [-Ealn] [-i extension] [-e script] … [-f script_file] … [file …]
any idea on what might be causing this?
Hi,
I love this script and i tried to make it a command in order to use it inside a script, without success.
It erases the output when it is use as a command (dump inside /usr/bin/xcol).
xcol command is not found inside a script when xcol it is add into the .bashrc
How could i use this xcol command inside a bash script ?
Thanks for this witty work