Skip to content

Instantly share code, notes, and snippets.

@definiteIymaybe
Last active February 14, 2024 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save definiteIymaybe/cb17fd549b3b1065416860c6d92dd78a to your computer and use it in GitHub Desktop.
Save definiteIymaybe/cb17fd549b3b1065416860c6d92dd78a to your computer and use it in GitHub Desktop.
input.log with lines prefixed with [YYYY-MM-DD] > YYYY-MM-DD.log files, with entries for respective dates
#!/bin/bash
# input: log lines prefixed with "[YYYY-MM-DD" e.g. "[2024-02-13]", "[2024-02-13 00:00:41]" etc.
# output: YYYY-MM-DD.log files, with entries for respective dates
# Input file
input_file=$1
gawk '
# For each line
{
# Assume the date is in the format [YYYY-MM-DD] and time is HH:MM:SS, both in the first two fields
# Extract the date directly without needing to trim brackets
split($1, parts, /[-\[\]]/)
year = parts[2]
month = parts[3]
day = parts[4]
# Format the date to get the year, month, and day
year_month_day = year "-" month "-" day
# Write the line to the new file named after the date
output_file = year_month_day ".log"
print $0 >> output_file
# Close the file to avoid too many open files
close(output_file)
}
' "$input_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment