Learn the fundamental gnaw commands and patterns for everyday use.
The most basic gnaw usage is searching for a pattern in a file:
gnaw "pattern" file.txt
4: 2023-03-15 12:34:58,789 ERROR django.request: Internal Server Error: /api/v1/data/
7: 2023-03-15 12:35:12,234 ERROR auth.middleware: Authentication failed for user 12345
Search through directories and subdirectories:
gnaw -r "pattern" ./logs/
logs/app.log:4: 2023-03-15 12:34:58,789 ERROR django.request: Internal Server Error
logs/error.log:1: 2023-03-15 12:35:12,234 ERROR auth.middleware: Authentication failed
gnaw -i "error" test.log
gnaw -n "pattern" file.txt
gnaw -c "pattern" file.txt
# Files with matches
gnaw -l "pattern" ./logs/
# Files without matches
gnaw -L "pattern" ./logs/
# Show 2 lines before and after each match
gnaw -C 2 "ERROR" test.log
# Show 3 lines after each match
gnaw -A 3 "ERROR" test.log
# Show 1 line before each match
gnaw -B 1 "ERROR" test.log
2: 2023-03-15 12:34:55,123 INFO app.startup: Application starting
3: 2023-03-15 12:34:56,456 INFO app.config: Loading configuration
4: 2023-03-15 12:34:58,789 ERROR django.request: Internal Server Error: /api/v1/data/
5: 2023-03-15 12:34:59,012 INFO app.recovery: Attempting recovery
6: 2023-03-15 12:35:00,345 INFO app.recovery: Recovery successful
--
8: 2023-03-15 12:35:10,123 INFO app.request: Processing request
9: 2023-03-15 12:35:12,234 ERROR auth.middleware: Authentication failed for user 12345
10: 2023-03-15 12:35:13,456 INFO app.response: Sending error response
gnaw -E "ERROR|WARN" test.log
gnaw -F "exact string" file.txt
gnaw -e "ERROR" -e "WARN" test.log
cat test.log | gnaw "ERROR"
gnaw "pattern" file1.txt file2.txt file3.txt
gnaw "pattern" *.log
gnaw "pattern" logs/*.log
gnaw --json "ERROR" test.log
{
"search_term": "ERROR",
"time_taken": "0.123s",
"results": [
{
"file_name": "test.log",
"line_number": 4,
"line": "2023-03-15 12:34:58,789 ERROR django.request: Internal Server Error"
}
]
}
gnaw --stream "pattern" large_file.log
gnaw --raw -c "pattern" file.txt
# Find all errors in today's logs
gnaw "ERROR" /var/log/app-$(date +%Y-%m-%d).log
# Count warnings
gnaw -c "WARN" /var/log/app.log
# Find errors with context
gnaw -C 3 "ERROR" /var/log/app.log
# Find function definitions
gnaw -n "def " *.py
# Find TODO comments
gnaw -n "TODO" src/
# Find imports
gnaw -n "import " *.py
# Find files containing a pattern
gnaw -l "database" src/
# Find files without a pattern
gnaw -L "test" src/
gnaw automatically respects ignore files:
.gitignore files.gnawignore files.ignore filesCreate a .gnawignore file:
# Ignore log files
*.log
# Ignore node_modules
node_modules/
# Ignore build directories
build/
dist/
gnaw --no-ignore "pattern" .