### grcat Configuration Entry Example Source: https://github.com/garabik/grc/blob/master/README.markdown An example of a grcat configuration entry demonstrating the use of 'regexp', 'colour', and 'count' keywords to color multiple occurrences of pathnames in green. ```grcat-config # this is probably a pathname regexp=/[w/.]+ colour=green count=more ``` -------------------------------- ### grcat Multi-line Block Example Source: https://github.com/garabik/grc/blob/master/README.markdown An example illustrating how to define a multi-line block for coloring, using 'count=block' and 'count=unblock' to color mail signatures red. ```grcat-config regexp=^-{1,2}\s{0,1}$ colours=red count=block - regexp=^\s{0,5}$ colours=default count=unblock ``` -------------------------------- ### Grc Command-Line Usage Examples Source: https://github.com/garabik/grc/blob/master/README.markdown Demonstrates typical ways to use the `grc` command with different tools and input sources. It shows how `grc` simplifies the process by automatically selecting configuration files for `grcat`. ```bash grcat conf.log < /var/log/syslog /usr/sbin/traceroute www.linux.org | grcat conf.traceroute grcat conf.esperanto < Fundamento.txt | less -r grc netstat grc ping hostname grc tail /var/log/syslog ``` -------------------------------- ### Grc with Tail for Log Monitoring Source: https://github.com/garabik/grc/blob/master/README.markdown Shows how to use `grc` in conjunction with `tail` to continuously monitor and colourise log files in real-time. It provides examples for both GNU and BSD versions of `tail`. ```bash # If you have GNU tail: tail --follow=name /var/log/syslog | grcat conf.log >/dev/tty12 # or, if you have recent BSD tail: tail -F /var/log/syslog | grcat conf.log >/dev/tty12 ``` -------------------------------- ### grcat Replace Keyword Example Source: https://github.com/garabik/grc/blob/master/README.markdown Demonstrates the 'replace' keyword in grcat configuration, showing how to transform a time format using Python's re.sub() with backreferences. ```grcat-config regexp=(\d\d):(\d\d):(\d\d) replace=\1h\2m\3s ``` -------------------------------- ### Grc Configuration with Multiple Attributes Source: https://github.com/garabik/grc/blob/master/README.markdown Shows an example of a `grc` configuration entry that applies multiple colour and style attributes (bold, blink, green) to a matched pattern, specifically for displaying pathnames. ```regex # this is probably a pathname regexp=/[w/.]+ colours=bold blink green count=more ``` -------------------------------- ### Fish Shell Integration for grc Source: https://context7.com/garabik/grc/llms.txt Configures the Fish shell to colorize command output automatically using grc. It supports sourcing a provided fish configuration or manual setup using a list of executables. ```fish # Add to ~/.config/fish/config.fish or ~/.config/fish/conf.d/grc.fish # Method 1: Source the provided fish configuration source /usr/local/etc/grc.fish # Method 2: Manual configuration set -U grc_plugin_execs ping netstat traceroute df du docker kubectl journalctl for executable in $grc_plugin_execs if type -q $executable function $executable --inherit-variable executable --wraps=$executable if isatty 1 grc $executable $argv else command $executable $argv end end end end ``` -------------------------------- ### GRC Count Modes for Matching Behavior Source: https://context7.com/garabik/grc/llms.txt The `count` keyword in GRC configuration files determines how regular expression matches are applied. Modes like `more` (apply to all matches), `once` (apply only to the first match), `block` (start a colored block), and `unblock` (end a colored block) offer granular control over the coloring process. ```ini # Example usage of count modes in a GRC configuration file # Apply to all occurrences on a line or across lines regexp=pattern_a colours=blue count=more ====== # Apply only to the first occurrence regexp=pattern_b colours=red count=once ====== # Start a multiline colored block regexp=START_BLOCK colours=green count=block ====== # End the multiline colored block regexp=END_BLOCK colours=default count=unblock ``` -------------------------------- ### Dynamic grc Alias Generation Script Source: https://context7.com/garabik/grc/llms.txt A bash script that dynamically generates `grc` aliases for installed programs that have corresponding grc configuration files. It iterates through common commands and grc config files to create aliases. ```bash #!/bin/bash # Generate grc aliases for all available configurations # Check for available config files and installed commands for cmd in g++ gas head make ld ping6 tail traceroute6 $(ls /usr/share/grc/); do cmd="${cmd##*conf.}" if type "${cmd}" >/dev/null 2>&1; then echo "alias ${cmd}=\"$(which grc)\" --colour=auto ${cmd}" fi done # Output can be added to shell rc file: # alias ping="grc --colour=auto ping" # alias netstat="grc --colour=auto netstat" # alias traceroute="grc --colour=auto traceroute" # ... ``` -------------------------------- ### Grc Configuration File Format Source: https://github.com/garabik/grc/blob/master/README.markdown Illustrates the structure of `grc` configuration files (`/etc/grc.conf` or `~/.grc/grc.conf`). Each entry consists of a regular expression on the first line and the corresponding `grcat` configuration file name on the second line. ```regex # log file \b\w+\b.*log\b conf.log # traceroute command (^|[/w\.]+/)traceroute\s conf.traceroute ``` -------------------------------- ### Grc Integration with Fish Shell Source: https://github.com/garabik/grc/blob/master/README.markdown Explains how to integrate `grc` with the Fish shell by sourcing the `grc.fish` script, usually found in `/usr/local/etc/grc.fish` or a similar path. ```fish source /usr/local/etc/grc.fish ``` -------------------------------- ### Grc Integration with Bash Shell Source: https://github.com/garabik/grc/blob/master/README.markdown Provides instructions for setting up automatic aliases for `grc` in a Bash environment by sourcing the `grc.sh` script from `/etc/profile.d/` or by setting `GRC_ALIASES=true` in `/etc/default/grc`. ```bash GRC_ALIASES=true [[ -s "/etc/profile.d/grc.sh" ]] && source /etc/grc.sh ``` -------------------------------- ### Grc Colour Options Source: https://github.com/garabik/grc/blob/master/README.markdown Lists the available colour and style attributes that can be used within `grc` configuration files to customize output highlighting. It also mentions additional, less portable attributes. ```text none, default, bold, underline, blink, reverse, concealed, black, green, yellow, blue, magenta, cyan, white, on_black, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white, beep dark, italic, rapidblink, strikethrough ``` -------------------------------- ### GRC Configuration File Format Source: https://context7.com/garabik/grc/llms.txt GRC configuration files use a simple `keyword=value` format separated by `======` delimiters. Keywords like `regexp`, `colours`, `count`, `skip`, and `replace` define how text should be matched and colorized. This allows for complex pattern matching, text substitution, and multiline block coloring. ```ini # Example configuration file: conf.custom # Match IP addresses and color them blue regexp=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} colours=bold blue count=more ====== # Match error keywords in red regexp=\b(error|fail|failed|failure)\b colours=bold red count=more ====== # Match success keywords in green regexp=\b(success|ok|passed|done)\b colours=green count=more ====== # Match timestamps with grouped colors regexp=(\d{4}-\d{2}-\d{2})\s+(\d{2}:\d{2}:\d{2}) colours=default, cyan, yellow count=once ====== # Match pathnames regexp=/\w/\.+ colours=magenta count=more ====== # Skip lines containing DEBUG regexp=.*DEBUG.* skip=yes ====== # Replace time format from HH:MM:SS to HHhMMmSSs regexp=(\d\d):(\d\d):(\d\d) replace=\1h\2m\3s colours=yellow ====== # Start a colored block on signature delimiter regexp=^-{2}\s*$ colours=red count=block - # End the colored block on empty lines regexp=^\s*$ colours=default count=unblock ``` -------------------------------- ### Basic Regex Matching in Python Source: https://github.com/garabik/grc/blob/master/Regexp.txt Demonstrates how to use Python's `re` module for basic regular expression matching. It shows how to search for patterns within strings. ```python import re pattern = r"^\w+" # Matches one or more word characters at the start of the string text = "Hello, world!" match = re.search(pattern, text) if match: print(f"Match found: {match.group(0)}") else: print("No match found.") # Example with a different pattern pattern_num = r"\d+" # Matches one or more digits text_with_num = "There are 12 apples and 3 oranges." matches = re.findall(pattern_num, text_with_num) print(f"Numbers found: {matches}") ``` -------------------------------- ### Grc Integration with Zsh Shell Source: https://github.com/garabik/grc/blob/master/README.markdown Details how to enable `grc` aliases for Zsh users by sourcing the `grc.zsh` script, typically located in `/etc/grc.zsh`. ```zsh [[ -s "/etc/grc.zsh" ]] && source /etc/grc.zsh ``` -------------------------------- ### Regex Quantifiers: Greedy vs. Non-Greedy in Python Source: https://github.com/garabik/grc/blob/master/Regexp.txt Illustrates the difference between greedy and non-greedy quantifiers ('*', '+', '?') in Python regular expressions. Greedy quantifiers match as much as possible, while non-greedy ones match as little as possible. ```python import re html_string = "
This is a bold paragraph.
" # Greedy match for .* betweenand
pattern_greedy = r".*
" match_greedy = re.search(pattern_greedy, html_string) print(f"Greedy match: {match_greedy.group(0) if match_greedy else 'No match'}") # Non-greedy match for .*? betweenand
pattern_non_greedy = r".*?
" match_non_greedy = re.search(pattern_non_greedy, html_string) print(f"Non-greedy match: {match_non_greedy.group(0) if match_non_greedy else 'No match'}") # Example with '*' quantifier text_star = "abbbc" pattern_star_greedy = r"ab*c" match_star_greedy = re.search(pattern_star_greedy, text_star) print(f"Greedy 'ab*c': {match_star_greedy.group(0) if match_star_greedy else 'No match'}") # Matches 'abbbc' text_star_2 = "ac" pattern_star_non_greedy = r"ab*?c" match_star_non_greedy = re.search(pattern_star_non_greedy, text_star_2) print(f"Non-greedy 'ab*?c': {match_star_non_greedy.group(0) if match_star_non_greedy else 'No match'}") # Matches 'ac' # Example with '+' quantifier text_plus = "abbbc" pattern_plus_greedy = r"ab+c" match_plus_greedy = re.search(pattern_plus_greedy, text_plus) print(f"Greedy 'ab+c': {match_plus_greedy.group(0) if match_plus_greedy else 'No match'}") # Matches 'abbbc' text_plus_2 = "ac" pattern_plus_non_greedy = r"ab+?c" match_plus_non_greedy = re.search(pattern_plus_non_greedy, text_plus_2) print(f"Non-greedy 'ab+?c': {match_plus_non_greedy.group(0) if match_plus_non_greedy else 'No match'}") # No match because 'b+' requires at least one 'b' ``` -------------------------------- ### Run Commands with GRC Wrapper Source: https://context7.com/garabik/grc/llms.txt The `grc` command wraps other commands to automatically apply colorization based on predefined configurations. It supports options to force or disable colorization, redirect stderr, and use specific configuration files. This is useful for making the output of common utilities like `netstat`, `ping`, `docker`, and `kubectl` more readable. ```bash # Basic usage - colorize common commands grc netstat -tuln grc ping -c 4 google.com grc ps aux grc tail -f /var/log/syslog grc docker ps grc kubectl get pods # Force colorization even when output is piped grc --colour=on ping -c 3 localhost | tee ping.log # Disable colorization grc --colour=off netstat -a # Auto-detect terminal (default behavior) grc --colour=auto df -h # Redirect and colorize stderr instead of stdout grc -e make 2>&1 # Redirect both stdout and stderr grc -e -s make # Use a specific configuration file grc -c conf.log cat /var/log/messages # Run command in pseudo-terminal (experimental) grc --pty vim ``` -------------------------------- ### grc Configuration Rules Source: https://context7.com/garabik/grc/llms.txt Defines rules for grc's colorization. Each rule includes a regular expression (regexp) to match, a color scheme (colours), and a count mode (once, more, stop, previous, block, unblock) to control how matches are handled. ```grcconfig # Match only the first occurrence on each line regexp=\berror\b colours=red count=once ====== ``` ```grcconfig # Match all occurrences on each line (default) regexp=\d+ colours=yellow count=more ====== ``` ```grcconfig # Stop processing further rules after this match regexp=^FATAL:.* colours=bold red on_white count=stop ====== ``` ```grcconfig # Use the same count mode as the previous line regexp=continuation.* colours=cyan count=previous ====== ``` ```grcconfig # Start a multiline colored block regexp=^