filter

#!/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>”


This content originally appeared on DEV Community and was authored by Query Filter

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

    # Convert date string to epoch seconds
    cmd = "date -d \"" $col "\" +%s"
    cmd | getline t
    close(cmd)

    # Print row if timestamp >= cutoff
    if(t >= cutoff) print
}'


This content originally appeared on DEV Community and was authored by Query Filter


Print Share Comment Cite Upload Translate Updates
APA

Query Filter | Sciencx (2025-11-28T16:07:42+00:00) filter. Retrieved from https://www.scien.cx/2025/11/28/filter/

MLA
" » filter." Query Filter | Sciencx - Friday November 28, 2025, https://www.scien.cx/2025/11/28/filter/
HARVARD
Query Filter | Sciencx Friday November 28, 2025 » filter., viewed ,<https://www.scien.cx/2025/11/28/filter/>
VANCOUVER
Query Filter | Sciencx - » filter. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/28/filter/
CHICAGO
" » filter." Query Filter | Sciencx - Accessed . https://www.scien.cx/2025/11/28/filter/
IEEE
" » filter." Query Filter | Sciencx [Online]. Available: https://www.scien.cx/2025/11/28/filter/. [Accessed: ]
rf:citation
» filter | Query Filter | Sciencx | https://www.scien.cx/2025/11/28/filter/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.