Skip to content

Instantly share code, notes, and snippets.

@franzwong
Created February 25, 2023 03:13
Show Gist options
  • Save franzwong/914f18fe80036ac526960f9cb85bd042 to your computer and use it in GitHub Desktop.
Save franzwong/914f18fe80036ac526960f9cb85bd042 to your computer and use it in GitHub Desktop.
Filter log lines with date range and match pattern
2023-02-25 14:32:01 INFO: Successfully connected to database server
2023-02-25 14:33:27 ERROR: Unable to process request: invalid input format
2023-02-25 14:35:11 WARNING: Disk space is running low - only 10% remaining
2023-02-25 14:38:09 DEBUG: Received response in 0.0025 seconds
2023-02-25 14:41:02 INFO: User login successful - username: jdoe, IP address: 192.168.1.10
2023-02-25 14:44:12 ERROR: Failed to establish connection with external service
2023-02-25 14:47:09 DEBUG: Processing data - record count: 1000
2023-02-25 14:51:05 INFO: System restart initiated by admin user - reason: security patch installation
2023-02-25 14:55:23 WARNING: High CPU usage detected - current usage: 90%
2023-02-25 14:59:14 ERROR: Request timed out - no response received from server.
#!/usr/bin/awk -f
{
log_datetime=$1" "$2
# string comparison on sortable date format
if (log_datetime >= begin && log_datetime <= end && $0 ~ pattern) {
print $0
}
}
#!/bin/bash
# date format of begin and end must match the records in log file
gawk -v begin="2023-02-25 14:30:00" -v end="2023-02-25 14:50:00" -v pattern="Failed" -f log_filter.awk log_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment