#!/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
gsub(/([AP]M)$/, " \\1", ts)
# Replace last colon before milliseconds with dot
gsub(/:([0-9]{3}) /, ".\\1 ", ts)
# Remove remaining milliseconds if still present
gsub(/:[0-9]{3}/, "", 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)