Skip to content

Instantly share code, notes, and snippets.

@xenithorb
Created February 10, 2017 15:51
Show Gist options
  • Save xenithorb/b723eb81136a5a555943b5d82c637929 to your computer and use it in GitHub Desktop.
Save xenithorb/b723eb81136a5a555943b5d82c637929 to your computer and use it in GitHub Desktop.
tail with highlight for matching lines with color
color_tail() { local file="$1"; shift; local awk_cmd="$(count=0; for i in "$@"; do ((count++)); printf '/%s/ {print "\\033[3%sm",$0,"\\033[39m"; next; }\n' "$i" "$count"; done; echo "{ print \$0 }")"; tail -n100 -f "$file" | eval awk \'"${awk_cmd}"\'; }
@xenithorb
Copy link
Author

xenithorb commented Feb 10, 2017

Intent:

Tail a log file and highlight keyword lines

Usage:

This is intended to be used as a bash profile function. Please add it to ~/.bashrc or equivalent environment files. You can test it by simply copying and pasting it into an interactive terminal session. You may have any number of hightlight words, but only one file.

color_tail <file> <highlight_word1> [<hightlight_word2>...] 
Example:
color_tail /var/log/messages CRYPTO_KEY SERVICE_STOP 

image

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