blob: d02c0dff21df4640bf53eccec16376e014992000 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#!/bin/bash
#
# Simple colorize for bash by means of sed
#
# Copyright 2008+2012 by Andreas Schamanek <andreas@schamanek.net>
# GPL licensed (see end of file) * Use at your own risk!
#
# Usage examples:
# tail -f somemaillog | mycolorize white '^From: .*' bell
# tail -f somemaillog | mycolorize white '^From: \/.*' green 'Folder: .*'
# tail -f somemaillog | mycolorize --unbuffered white '^From: .*'
#
# Notes:
# Regular expressions need to be suitable for _sed --regexp-extended_
# Slashes / need no escaping (we use ^A as delimiter).
# \/ splits the coloring (similar to procmailrc. Matches behind get color.
# Even "white '(for|to) \/(her|him).*$'" works :) Surprisingly ;)
# To color the string '\/' use the regular expression '\\()/'.
# If the 1st argument is -u or --unbuffered, _sed_ will be run so.
# For the colors see tput(1) and terminfo(5), or e.g.
# https://wiki.archlinux.org/index.php/Color_Bash_Prompt
bold=$(tput bold) # make colors bold/bright
#normal=$(tput sgr0) # normal text
normal=$(echo -e "\e[0m") # (works better sometimes)
red="$bold$(tput setaf 1)" # bright red text
green=$(tput setaf 2) # dim green text
fawn=$(tput setaf 3); beige="$fawn" # dark yellow text
yellow="$bold$fawn" # bright yellow text
darkblue=$(tput setaf 4) # dim blue text
blue="$bold$darkblue" # bright blue text
purple=$(tput setaf 5); magenta="$purple" # magenta text
pink="$bold$purple" # bright magenta text
darkcyan=$(tput setaf 6) # dim cyan text
cyan="$bold$darkcyan" # bright cyan text
gray=$(tput setaf 7) # dim white text
white="$bold$gray" # bright white text
bell=$(tput bel) # bell/beep
# Make output unbuffered? (Pass argument -u|--unbuffered to _sed_.)
if [ "/$1/" = '/-u/' -o "/$1/" = '/--unbuffered/' ] ; then
UNBUFFERED='-u'; shift
else
UNBUFFERED=""
fi
# produce separator character ^A (for _sed_)
A=$(echo | tr '\012' '\001')
# compile all rules given at command line to 1 set of rules for SED
while [ "/$1/" != '//' ] ; do
c1=''; re=''; beep=''
c1=$1 ; re="$2" ; shift 2
# if a beep is requested in the optional 3rd parameter set $beep
[ "/$1/" != '//' ] && [[ ( "$1" = 'bell' || "$1" = 'beep' ) ]] \
&& beep=$bell && shift
# if the regular expression includes \/ we split the substitution
if [ "/${re/*\\\/*/}/" = '//' ] ; then
# we need to count "("s before the \/ (=$left)
left="${re%\\/*}"; leftlength=${#left}
# first we count "\("
dummy=${left//\\(}; escdgroups=$(( (leftlength-${#dummy})/2 ))
# now "(" (and we add 2 for the groups used for ($re) in $sedrules)
dummy=${left//(}; groupcnt=$((leftlength-${#dummy}-escdgroups+2))
# replace \/ with )( so below we get (left-re)(right-re)
re="${re/\\\//)(}"
sedrules="$sedrules;s$A($re)$A\1${!c1}\\$groupcnt$beep$normal${A}g"
else
sedrules="$sedrules;s$A($re)$A${!c1}\1$beep$normal${A}g"
fi
done
# call sed to do the main job
sed $UNBUFFERED --regexp-extended -e "$sedrules"
exit
# 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
|