Last active
August 12, 2024 16:58
-
-
Save meetnick/eccc5dfea67a6baa2300cb4853e575d6 to your computer and use it in GitHub Desktop.
Convert seconds past datetime to datetime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to calculate datetime after a given number of seconds from a starting datetime | |
calculate_datetime() { | |
local start_datetime="$1" | |
local elapsed_seconds="$2" | |
# Convert the start datetime to seconds since the epoch | |
local start_epoch=$(date -d "$start_datetime" +%s) | |
# Add the elapsed seconds (including fractional part) | |
local result_epoch=$(echo "$start_epoch + $elapsed_seconds" | bc) | |
# Convert the result back to a human-readable date-time format (without fractional seconds) | |
local result_datetime=$(date -d "@$result_epoch" +"%Y-%m-%d %H:%M:%S") | |
# Output the resulting datetime | |
echo "$result_datetime" | |
} | |
# Example usage of the function | |
# Uncomment the lines below to test the function directly in the script | |
#start_datetime="2024-08-12 08:00:00" | |
#elapsed_seconds="45736.799" | |
#result=$(calculate_datetime "$start_datetime" "$elapsed_seconds") | |
#echo "The resulting datetime is: $result" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to calculate datetime after a given number of seconds from a starting datetime | |
calculate_datetime() { | |
local start_datetime="$1" | |
local elapsed_seconds="$2" | |
# Convert the start datetime to seconds since the epoch | |
local start_epoch=$(date -d "$start_datetime" +%s) | |
# Add the elapsed seconds (including fractional part) | |
local result_epoch=$(echo "$start_epoch + $elapsed_seconds" | bc) | |
# Convert the result back to a human-readable date-time format (without fractional seconds) | |
local result_datetime=$(date -d "@$result_epoch" +"%Y-%m-%d %H:%M:%S") | |
# Output the resulting datetime | |
echo "$result_datetime" | |
} | |
# Check if the correct number of arguments is provided | |
if [ $# -ne 2 ]; then | |
echo "Usage: $0 <log_file> <initial_datetime>" | |
echo "Example: $0 /path/to/log_file.txt '2024-08-12 08:00:00'" | |
exit 1 | |
fi | |
# Assign arguments to variables | |
log_file="$1" | |
initial_datetime="$2" | |
# Check if the log file exists | |
if [ ! -f "$log_file" ]; then | |
echo "Error: Log file not found!" | |
exit 1 | |
fi | |
# Process each line in the log file | |
while IFS= read -r log_line; do | |
# Extract the time value (e.g., 12.592) from the log line using a regular expression | |
elapsed_seconds=$(echo "$log_line" | grep -oP '\[\s*\K[\d\.]+') | |
# If a valid elapsed time is found, calculate the corresponding datetime | |
if [ ! -z "$elapsed_seconds" ]; then | |
datetime=$(calculate_datetime "$initial_datetime" "$elapsed_seconds") | |
# Replace the time value in the log line with the calculated datetime | |
new_log_line=$(echo "$log_line" | sed "s/\[\s*$elapsed_seconds\]/[$datetime]/") | |
# Output the modified log line | |
echo "$new_log_line" | |
else | |
# If no time value is found, print the line as is | |
echo "$log_line" | |
fi | |
done < "$log_file" |
curl https://gist.githubusercontent.com/meetnick/eccc5dfea67a6baa2300cb4853e575d6/raw/e70f1dc5525c5f0cb56144e52ce3ab9750aeb65a/process_log_file.sh | bash -s /var/log/Xorg.0.log "2024-08-07 11:44:32"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's how to use it:
chmod +x process_log_file.sh
./process_log_file.sh /path/to/your_log_file.txt '2024-08-12 08:00:00'