Skip to content

Instantly share code, notes, and snippets.

@liufuyang
Last active April 15, 2024 02:31
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save liufuyang/df592fe3e9d77f53db45639ad1b21619 to your computer and use it in GitHub Desktop.
Save liufuyang/df592fe3e9d77f53db45639ad1b21619 to your computer and use it in GitHub Desktop.
Color output of linux tail command
#!/bin/bash
# save this file as tailc then
# run as: $ tailc logs/supplier-matching-worker.log Words_to_highlight
file=$1
if [[ -n "$2" ]]; then
color='
// {print "\033[37m" $0 "\033[39m"}
/(WARN|WARNING)/ {print "\033[1;33m" $0 "\033[0m"}
/(ERROR|CRIT)/ {print "\033[1;31m" $0 "\033[0m"}
/('$2')/ {print "\033[1;32m" $0 "\033[0m"}
'
else
color='
// {print "\033[37m" $0 "\033[39m"}
/(WARN|WARNING)/ {print "\033[1;33m" $0 "\033[0m"}
/(ERROR|CRIT)/ {print "\033[1;31m" $0 "\033[0m"}
'
fi
tail -5000f $file | awk "$color"
# Colors
# 30 - black 34 - blue 40 - black 44 - blue
# 31 - red 35 - magenta 41 - red 45 - magenta
# 32 - green 36 - cyan 42 - green 46 - cyan
# 33 - yellow 37 - white 43 - yellow 47 - white
@FunkyFinch
Copy link

Can you explain what everything is doing in the if statement?

@liufuyang
Copy link
Author

@FunkyFinch I think it is for handle different cases - whether you have a key word used when calling tailc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment