Skip to main content

Multiple Patterns

OR Logic (Regex Alternation)

ag uses PCRE by default, so the pipe | operator works natively:

# Lines containing "error" OR "warning"
ag "error|warning" app.log

# Any import of React, Vue, or Angular
ag "import (React|Vue|Angular)" src/

AND Logic (Chained ag)

ag has no built-in AND. Chain two ag calls:

# Lines containing BOTH "user" AND "failed"
ag "user" auth.log | ag "failed"

# 3-way: nginx + upstream + timeout
ag "nginx" /var/log/syslog | ag "upstream" | ag "timeout"

NOT Logic (-v)

# All lines that are NOT debug messages
ag -v "DEBUG" app.log

# Active config: skip comments and blank lines
ag -v "^\s*(#|$)" /etc/nginx/nginx.conf

Reading Patterns from a File (-f)

If you have many patterns to match, store them in a file:

# blocked_terms.txt
# api_key
# password
# secret

ag -f blocked_terms.txt /var/www/

Each line is a separate PCRE pattern, combined with implicit OR.