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.
- use a command followed by a filename and print the contents to the terminal (using bash would be ideal)
- 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
- 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.