1

Hello, I have this part of a script where I would like to get only the rows where the date (column 3) is older than the 10/30/2002 (format mm/dd/yyyy), but I'm not able to get the data correctly. I tried different things like store the value on a variable and call the $ or put it inside a () or like a string but nothing happens. The data type of the column $3 is "date".

Could you help me? How I am supposed to filter only for older values than that date?

#!/bin/bash
IFS=',' records=() sorted=()
{
    IFS='' read -r header

    while read -r -a values
    do
        [[ ${values[3]} < "10/30/2002" ]] || continue

        case....
Joseph
  • 11
  • 2
  • Please, take some time to read [How to create a Minimal, Reproducible Example](/help/minimal-reproducible-example) and [how to ask](/help/how-to-ask) – LMC May 29 '22 at 22:45
  • 1
    If you change your date formatting to yyyy-mm-dd it becomes trivial. – Shawn May 30 '22 at 00:31
  • And bash doesn't really have data types. Strings and arrays of strings. – Shawn May 30 '22 at 00:33
  • You can search for how to do date arithmetic here, you will find complete examples. – Nic3500 May 30 '22 at 00:41
  • Look at [Efficient way to convert date time string to epoch in bash](https://stackoverflow.com/a/49195703/1765658) – F. Hauri May 30 '22 at 06:40

1 Answers1

0

If you can't change the format, as mentioned in a comment, convert to seconds and use it in comparison (notice that the options to the date command can be different, this is for macOS)

#! /bin/bash

to_s() {
    local f='%m/%d/%Y %H:%M:%S'
    echo "$(date -ujf "$f" "$1 00:00:00" +'%s')"
}

s=$(to_s '10/30/2002')

while read -r -a values
do
    [[ $(to_s "${values[3]}") -lt $s ]] || continue
    # ...
Diego Torres Milano
  • 61,192
  • 8
  • 106
  • 129
  • Instead of running 1 fork for each conversion, consider [running `date -f - +%s` as backgrounded separated job](https://stackoverflow.com/a/49195703/1765658)!! – F. Hauri May 30 '22 at 06:37
  • Hi @DiegoTorres, in this case, the rest of values from $3 will have the old format? Or with the added code at the beggining we change all $3? – Joseph May 30 '22 at 16:44
  • @Joseph I don't know what's the format, but you can adapt the conversion to your needs – Diego Torres Milano May 30 '22 at 20:09