1

I want is to cat a log file and highlight certain lines with color based on a pattern match but to ignore case. For example:

  • When the log file has the word success then make that line green.
  • When it finds error make that line red etc.

This is what I have so far. It is based on cat and awk (although when I used man awk I got man for gawk instead). I am using Debian Jessie.

 #!/bin/bash

[ $# -ge 1 -a -f "$1" ] && logfile="$1" || logfile="-"
cat $logfile | awk '
/install/ {print "\033[32m" $0 "\033[39m"}
/success/ {print "\033[32m" $0 "\033[39m"}
/status/ {print "\033[33m" $0 "\033[39m"}
/info/ {print "\033[33m" $0 "\033[39m"}
/error/ {print "\033[31m" $0 "\033[39m"}
/fail/ {print "\033[31m" $0 "\033[39m"}
'

I have used all manner of stuff, but I end up with all my text in 1 colour, no colour, or the case is not being ignored. Things like IGNORECASE=1 don't seem to work.

I am happy to use something else, as I just pinched this script from another question. Below is exactly what I want in case someone wants to suggest a new script.

  1. use a command followed by a filename and print the contents to the terminal (using bash would be ideal)
  2. in my script I would like to add strings to match and make those lines in a log/text file a certain color when printed to the screen. eg. find the word error and make that line red
  3. ignore case for the words I want to match, eg. find the word error, ERROR, or any combination of upper lower case, like Error, or ERRor etc. Then print that line to the screen in a red color.

The idea is to be able to easily spot important parts in some of my backup, and things like apache access logs or whatever. I intend to find the important key words I am after and then simply adding those in my script.

Below is a snippet of the beginning and end of one of log files as an example:

INFO: tar attempt number 0
/home/user/domains/
/home/user/domains/website.com/
/home/user/domains/website.com/logs/
/home/user/domains/website.com/logs/Apr-2015.tar.gz.4
/home/user/domains/website.com/logs/Apr-2015.tar.gz
/home/user/domains/website.com/logs/Apr-2015.tar.gz.1
/home/user/domains/website.com/logs/Apr-2015.tar.gz.3
/home/user/domains/website.com/logs/Apr-2015.tar.gz.2
/home/user/domains/website.com/public_html/
/home/user/domains/website.com/public_html/api.php
/home/user/domains/website.com/public_html/index.html
/home/user/domains/website.com/public_html/favicon.ico
/home/user/domains/website.com/public_html/favicon1.ico
SUCCESS: website tar exists.
INFO: DB backup dump, attempt number 0
SUCCESS: DB backup file created.
/home/user/dakka_backups/user_db-Thu.2015-04-30.sql
INFO: CN Website and DB backed up on Thu.2015-04-30.
  • Can you share a snippet of your logfile also ? – amisax Jul 15 '15 at 4:05
  • I've added an example to the end of my question. I was intending to check out a few logs like apache access logs, ssh logs etc and define a few key words I want to have coloured. So ultimately I wanted to use this against all kinds of log files. – dakka Jul 15 '15 at 4:52
3

Awk has a function called 'tolower':

cat $logfile | awk '
     tolower($0) ~ /install/ {print "\033[32m" $0 "\033[39m"; next;}
     tolower($0) ~ /success/ {print "\033[32m" $0 "\033[39m"; next;}
     tolower($0) ~ /status/ {print "\033[33m" $0 "\033[39m";  next;}
     tolower($0) ~ /info/ {print "\033[33m" $0 "\033[39m";    next;}
     tolower($0) ~ /error/ {print "\033[31m" $0 "\033[39m";   next;}
     tolower($0) ~ /fail/ {print "\033[31m" $0 "\033[39m";    next;}
     { print; }
'

With Bash, you can use shopt -s nocasematch for case insensitive pattern matching. eg.

mystring="AbCdEfG"
shopt -s nocasematch
if [[ ${mystring} == abcdefg ]] ; then
     echo "is a match!!"
fi
  • Almost! That displays only what I am matching, is there a way to match everything else without changing the colours? I want the log to print out normally like when you use cat filename except what my script matches should be in different colours...hope that made sense? – dakka Jul 15 '15 at 4:46
  • @dakka - I've updated the answer. – DarkHeart Jul 15 '15 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.