examples / common-patterns

Common Patterns

Real-world examples of gnaw usage for common development tasks.

Log Analysis

Error Monitoring

Monitor application logs for errors in real-time:

# Monitor errors in real-time
tail -f /var/log/app.log | gnaw --stream "ERROR"

# Count errors by hour
gnaw -c "ERROR" /var/log/app-$(date +%Y-%m-%d).log

# Find errors with context
gnaw -C 3 "ERROR" /var/log/app.log

Performance Analysis

Analyze performance metrics from logs:

# Find slow queries
gnaw "slow query" /var/log/db.log

# Monitor response times
gnaw -E "response_time:[0-9]{4,}" /var/log/app.log

# Find memory issues
gnaw -i "out of memory\|memory leak" /var/log/app.log

Security Monitoring

Detect security issues in logs:

# Find failed login attempts
gnaw -i "authentication failed\|login failed" /var/log/auth.log

# Monitor for suspicious activity
gnaw -E "failed.*attempts.*[0-9]{3,}" /var/log/security.log

# Check for SQL injection attempts
gnaw -i "union.*select\|drop.*table" /var/log/web.log

Code Analysis

Code Quality Checks

Ensure code quality standards:

# Find TODO comments
gnaw -n "TODO" src/

# Check for debug statements
gnaw -l "console\.log\|print\|debugger" src/

# Find hardcoded credentials
gnaw -i "password.*=\|api_key.*=\|secret.*=" src/

Documentation Analysis

Analyze code documentation:

# Find undocumented functions
gnaw -l "def " src/ | gnaw -v "docstring\|@doc"

# Check for missing docstrings
gnaw -A 2 "def " src/ | gnaw -v "docstring"

# Find example code
gnaw -i "example\|usage\|demo" docs/

Dependency Analysis

Analyze code dependencies:

# Find import statements
gnaw -n "import " src/

# Check for unused imports
gnaw -l "import " src/ | gnaw -v "used"

# Find external dependencies
gnaw -E "from.*import\|require.*" src/

File Management

File Discovery

Find files based on content:

# Find configuration files
gnaw -l "config\|settings" .

# Find test files
gnaw -l "test\|spec" .

# Find documentation files
gnaw -l "readme\|doc\|guide" .

Content Analysis

Analyze file contents:

# Find large files
find . -type f -size +10M | gnaw -l "large file"

# Find binary files
file * | gnaw -i "binary\|executable"

# Find empty files
find . -type f -empty | gnaw -l "empty file"

System Administration

System Monitoring

Monitor system health:

# Check disk usage
df -h | gnaw -E "[0-9]{2,}%"

# Monitor memory usage
free -h | gnaw -E "[0-9]+G"

# Check running processes
ps aux | gnaw -i "python\|node\|java"

Service Management

Manage system services:

# Check service status
systemctl status | gnaw -i "active\|failed"

# Monitor service logs
journalctl -u nginx | gnaw "error\|warning"

# Find service configurations
gnaw -l "service\|daemon" /etc/systemd/system/

Development Workflow

Git Integration

Integrate with Git workflows:

# Check for sensitive data in commits
git log --oneline | gnaw -i "password\|secret\|key"

# Find large files in history
git log --stat | gnaw -E "[0-9]+ files changed"

# Monitor commit messages
git log --oneline | gnaw -i "fix\|bug\|error"

Build Process

Monitor build processes:

# Check build logs
gnaw -i "error\|warning\|failed" build.log

# Monitor compilation
gnaw -i "compiling\|building\|linking" build.log

# Find build artifacts
gnaw -l "\.o\|\.so\|\.a" build/

Data Processing

CSV Analysis

Analyze CSV data:

# Find specific values
gnaw -F "specific_value" data.csv

# Count occurrences
gnaw -c "pattern" data.csv

# Extract specific columns
gnaw -o "column_name.*" data.csv

JSON Processing

Process JSON data:

# Find specific keys
gnaw -o "\"key\":.*" data.json

# Extract values
gnaw -o "\"value\":.*" data.json

# Find nested objects
gnaw -E "\{[^}]*\}" data.json

XML Processing

Process XML data:

# Find specific tags
gnaw -o "<tag>.*</tag>" data.xml

# Extract attributes
gnaw -o "attribute=\".*\"" data.xml

# Find nested elements
gnaw -E "<[^>]*>.*</[^>]*>" data.xml

Network Analysis

Log Analysis

Analyze network logs:

# Find failed connections
gnaw -i "connection failed\|timeout" /var/log/network.log

# Monitor bandwidth usage
gnaw -E "bytes.*[0-9]{6,}" /var/log/network.log

# Check for security issues
gnaw -i "blocked\|denied\|forbidden" /var/log/firewall.log

API Monitoring

Monitor API endpoints:

# Find API errors
gnaw -E "HTTP/[0-9]+.*[4-5][0-9][0-9]" /var/log/api.log

# Monitor response times
gnaw -E "response_time:[0-9]+ms" /var/log/api.log

# Check for rate limiting
gnaw -i "rate limit\|throttle" /var/log/api.log

Best Practices

- Use specific patterns for better performance - Limit search scope when possible - Use streaming for large files - Consider file size thresholds - Use case-insensitive search when appropriate - Include context for better understanding - Verify results manually - Use multiple patterns for comprehensive analysis - Document complex patterns - Use configuration files for repeated patterns - Monitor performance over time - Update patterns as needed

Advanced Examples

Complex Pattern Matching

# Find email addresses
gnaw -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" data.txt

# Find IP addresses
gnaw -E "([0-9]{1,3}\.){3}[0-9]{1,3}" data.txt

# Find URLs
gnaw -E "https?://[^\s]+" data.txt

Multi-file Analysis

# Analyze multiple log files
gnaw -r "ERROR" /var/log/

# Compare different time periods
gnaw "ERROR" /var/log/app-2023-03-15.log
gnaw "ERROR" /var/log/app-2023-03-16.log

# Find patterns across different file types
gnaw -r "pattern" --type log,txt,csv .

Automated Reporting

#!/bin/bash
# Generate daily error report
DATE=$(date +%Y-%m-%d)
ERROR_COUNT=$(gnaw --raw -c "ERROR" /var/log/app-$DATE.log)
WARN_COUNT=$(gnaw --raw -c "WARN" /var/log/app-$DATE.log)

echo "Daily Report - $DATE"
echo "Errors: $ERROR_COUNT"
echo "Warnings: $WARN_COUNT"

if [ "$ERROR_COUNT" -gt 10 ]; then
    echo "High error count detected!"
    gnaw -C 2 "ERROR" /var/log/app-$DATE.log
fi
These examples demonstrate common use cases for gnaw. Adapt them to your specific needs and environment.