bash ignorecase match pattern in file and color that line, print all to screen

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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 "33[32m" $0 "33[39m"
/success/ print "33[32m" $0 "33[39m"
/status/ print "33[33m" $0 "33[39m"
/info/ print "33[33m" $0 "33[39m"
/error/ print "33[31m" $0 "33[39m"
/fail/ print "33[31m" $0 "33[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.









share|improve this question
























  • 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















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 "33[32m" $0 "33[39m"
/success/ print "33[32m" $0 "33[39m"
/status/ print "33[33m" $0 "33[39m"
/info/ print "33[33m" $0 "33[39m"
/error/ print "33[31m" $0 "33[39m"
/fail/ print "33[31m" $0 "33[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.









share|improve this question
























  • 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













1












1








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 "33[32m" $0 "33[39m"
/success/ print "33[32m" $0 "33[39m"
/status/ print "33[33m" $0 "33[39m"
/info/ print "33[33m" $0 "33[39m"
/error/ print "33[31m" $0 "33[39m"
/fail/ print "33[31m" $0 "33[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.









share|improve this question
















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 "33[32m" $0 "33[39m"
/success/ print "33[32m" $0 "33[39m"
/status/ print "33[33m" $0 "33[39m"
/info/ print "33[33m" $0 "33[39m"
/error/ print "33[31m" $0 "33[39m"
/fail/ print "33[31m" $0 "33[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.






shell-script text-processing awk logs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 13 at 22:14









Rui F Ribeiro

39.7k1479132




39.7k1479132










asked Jul 15 '15 at 1:39









dakkadakka

646411




646411












  • 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

















  • 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
















Can you share a snippet of your logfile also ?

– amisax
Jul 15 '15 at 4:05





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





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










1 Answer
1






active

oldest

votes


















3














Awk has a function called 'tolower':



cat $logfile | awk '
tolower($0) ~ /install/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /success/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /status/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /info/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /error/ print "33[31m" $0 "33[39m"; next;
tolower($0) ~ /fail/ print "33[31m" $0 "33[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





share|improve this answer

























  • 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.

    – LukeM
    Jul 15 '15 at 4:53











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f216054%2fbash-ignorecase-match-pattern-in-file-and-color-that-line-print-all-to-screen%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Awk has a function called 'tolower':



cat $logfile | awk '
tolower($0) ~ /install/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /success/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /status/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /info/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /error/ print "33[31m" $0 "33[39m"; next;
tolower($0) ~ /fail/ print "33[31m" $0 "33[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





share|improve this answer

























  • 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.

    – LukeM
    Jul 15 '15 at 4:53
















3














Awk has a function called 'tolower':



cat $logfile | awk '
tolower($0) ~ /install/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /success/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /status/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /info/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /error/ print "33[31m" $0 "33[39m"; next;
tolower($0) ~ /fail/ print "33[31m" $0 "33[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





share|improve this answer

























  • 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.

    – LukeM
    Jul 15 '15 at 4:53














3












3








3







Awk has a function called 'tolower':



cat $logfile | awk '
tolower($0) ~ /install/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /success/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /status/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /info/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /error/ print "33[31m" $0 "33[39m"; next;
tolower($0) ~ /fail/ print "33[31m" $0 "33[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





share|improve this answer















Awk has a function called 'tolower':



cat $logfile | awk '
tolower($0) ~ /install/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /success/ print "33[32m" $0 "33[39m"; next;
tolower($0) ~ /status/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /info/ print "33[33m" $0 "33[39m"; next;
tolower($0) ~ /error/ print "33[31m" $0 "33[39m"; next;
tolower($0) ~ /fail/ print "33[31m" $0 "33[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






share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 15 '15 at 6:03

























answered Jul 15 '15 at 4:35









LukeMLukeM

3,44932340




3,44932340












  • 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.

    – LukeM
    Jul 15 '15 at 4:53


















  • 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.

    – LukeM
    Jul 15 '15 at 4:53

















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





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.

– LukeM
Jul 15 '15 at 4:53






@dakka - I've updated the answer.

– LukeM
Jul 15 '15 at 4:53


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f216054%2fbash-ignorecase-match-pattern-in-file-and-color-that-line-print-all-to-screen%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay