Convert this script where I can input the day

Joined
Apr 16, 2023
Messages
149
Reaction score
16
Credits
1,460
Code:
LC_ALL=C awk -v beg=10:00:00 -v end=13:00:00 '
  match($0, /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/) {
    t = substr($0, RSTART, 8)
    if (t >= end) selected = 0
    else if (t >= beg) selected = 1
  }
  selected'

I didn't write this script but I vaguely understand it in gist and what it does..
The date format in our logs is like this (first and second column then rest are stuffs like INFO/DEBUG name of class bla bla...

Code:
2023-09-13 20:21:28

What I want
I want to be able to input the beginning day as well i.e I want logs of 2023-09-12 from 05:30:00 to 13:30:00.
How'd I do it?
I vaguely know it requires some regex and regex I bad at it.
 


Something like this?

LC_ALL=C awk -v beg_date="2023-09-13" -v end_date="2023-09-15" -v beg_time="10:00:00" -v end_time="13:00:00" '
match($0, /[0-9]{4}-[0-9]{2}-[0-9]{2}/) {
date = substr($0, RSTART, 10)
}
match($0, /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/) {
time = substr($0, RSTART, 8)
}
date >= beg_date && date <= end_date && time >= beg_time && time <= end_time
'
 
I was going by the script you already had. If I was starting from scratch, I would do something like this.

#!/bin/bash

# Prompt the user to enter a start date and time
read -p "Enter a start date and time (YYYY-MM-DD HH:MM:SS): " start_date

# Prompt the user to enter an end date and time
read -p "Enter an end date and time (YYYY-MM-DD HH:MM:SS): " end_date

# Convert the user input to Unix timestamps
start_timestamp=$(date -d "$start_date" +%s)
end_timestamp=$(date -d "$end_date" +%s)

# Check if the conversion was successful
if [ $? -ne 0 ]; then
echo "Invalid date and time format. Please use YYYY-MM-DD HH:MM:SS."
exit 1
fi

# Specify the directory you want to list files from
directory="/path/to/your/directory"

# List files in the directory and sort by modification time
find "$directory" -type f -printf "%T@ %p\n" | sort -n | while read -r file; do
# Extract the file timestamp and path
file_timestamp=$(echo "$file" | awk '{print $1}')
file_path=$(echo "$file" | cut -d ' ' -f 2-)

# Check if the file's timestamp is within the specified range
if [ "$file_timestamp" -ge "$start_timestamp" ] && [ "$file_timestamp" -le "$end_timestamp" ]; then
echo "$file_path"
fi
done
 

Members online


Top