fork download
  1. #!/bin/bash
  2.  
  3. set -- /etc/debian_version "[00:00:00:000][01-01-2024]" "[23:59:59:999][31-12-2024]"
  4.  
  5. # Check if correct number of arguments are passed
  6. if [ "$#" -ne 3 ]; then
  7. echo "Usage: $0 <log_file_name> <start_timestamp> <end_timestamp>"
  8. echo "Timestamps should be in the format '[HH:MM:SS:SSS][DD-MM-YYYY]'"
  9. exit 1
  10. fi
  11.  
  12. log_file=$1
  13. start_timestamp=$2
  14. end_timestamp=$3
  15.  
  16. # Check if log file exists
  17. if [ ! -f "$log_file" ]; then
  18. echo "File not found: $log_file"
  19. exit 1
  20. fi
  21.  
  22. awk -v start="$start_timestamp" -v end="$end_timestamp" '
  23. function parsedate(date) {
  24. split(date, a, /[]:[-]+/)
  25. return a[8] "-" a[7] "-" a[6] "T" a[2] ":" a[3] ":" a[4] "." a[5]
  26. }
  27. BEGIN {
  28. st = parsedate(start)
  29. et = parsedate(end)
  30. }
  31. ((p = parsedate($0)) >= st) && (p <= et)' <<\:
  32. [01:01:01:001][31-12-2023] First log line
  33. [02:02:02:002][01-01-2024] Second log line
  34. [03:03:03:003][31-12-2024] Third log line
  35. [04:04:04:004][01-01-2025] Fourth log line
  36. :
  37.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
[02:02:02:002][01-01-2024] Second log line
[03:03:03:003][31-12-2024] Third log line