RSS
people

Highlight terms while tailing a log

Sometimes I want to keep tailing a log and look for a term – for example if you are an e-commerce platform then you may like to keep a running tail somewhere while keeping en eye on “checkout” page.

Following code snippet will highlight the term:

tail -f /var/log/apache2/access.log | perl -p -e 's/(checkout)/\033[46;1m$1\033[0m/g;'

You can highlight two such terms as well:

tail -f /var/log/apache2/access.log | perl -p -e 's/(checkout|another)/\033[46;1m$1\033[0m/g;'

Any perl regex should work for the highlighting, if your terminal supports it.

No Comments | Tags: , , , ,

Basic regex reference

OperatorPurpose
. (period)Match any single character
^ (caret)Match the empty string that occurs at the beginning of a line or string
$ (dollar sign)Match the empty string that occurs at the end of a line
AMatch an uppercase letter A
aMatch a lowercase letter a
\dMatch any single digit
\DMatch any single nondigit character
\wMatch any single alphanumeric character; a synonym is [:alnum:]
[A-E]Match any of uppercase A, B, C, D, or E
[^A-E]Match any character except uppercase A, B, C, D, or E
X?Match none or one capital letter X
X*Match zero or more capital Xes
X+Match one or more capital Xes
X{n}Match exactly n capital Xes
1 Comment | Tags: