• Tag Archives grep
  • Highlight function with grep

    It’s been a while since I have been blogging, so I will have to start fresh.

    I recently came across this gem which defines a bash function named “highlight” which will highlight the first argument in all subsequent files. if only 1 argument is given, it expects console input for highlighting.

    /usr/local/bin/highlight

    #!/bin/bash
    
    highlight()
    {
      if [ 1 -eq $# ];
      then
        grep --color -E "$1|$"
      else
        grep --color -E "$1|$" "${@:1}"
      fi
    }
    
    highlight $@

    Realworld examples:

    tail -f /var/log/messages|highlight sshd
    tail -f /var/log/audit/audit.log|grep avc||highlight denied
    highlight “failed” /var/log/secure