#!/bin/bash
# Usage: cat file.csv | ./filter_lastNdays.sh <date_column_number> <days>
# Example: cat file.csv | ./filter_lastNdays.sh 4 45
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 <date_column_number> <days>"
exit 1
fi
DATE_COL="$1"
DAYS="$2"
# Calculate cutoff timestamp (N days ago) in epoch seconds
CUTOFF=$(date -d "-$DAYS days" +%s)
awk -F'~' -v col="$DATE_COL" -v cutoff="$CUTOFF" '
{
ts = $col
# Add space before AM/PM if missing
if (ts ~ /AM$/ || ts ~ /PM$/) {
ts = gensub(/(AM|PM)$/, " \\1", "g", ts)
}
# Replace last colon before milliseconds with dot
ts = gensub(/:([0-9]{3})(AM|PM)?$/, ".\\1\\2", 1, ts)
# Remove any remaining milliseconds colons just in case
gsub(/:([0-9]{3})/, ".\\1", ts)
# Convert to epoch seconds
cmd = "date -d \"" ts "\" +%s"
cmd | getline t
close(cmd)
# Print row if timestamp >= cutoff
if(t >= cutoff) print
}'
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)