#!/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
CUTOFF=$(date -d "-$DAYS days" +%s)
awk -F'~' -v col="$DATE_COL" -v cutoff="$CUTOFF" '
{
ts = $col
# Remove milliseconds completely (":123")
gsub(/:[0-9]{3}/, "", ts)
# Ensure a space before AM/PM if missing
gsub(/([AP]M)$/, " \\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)