filter date from a partiicular column (from the last 3rd column) of a csv file. Also if the last 3rd column is empty then print lines

Input csv file is

23,54,one,tree,56,23,7/20/2017 17:00,iii,89
23,54,one,tree,56,23,7/20/2017 17:00,iii,89
23,54,one,tree,56,23,7/21/2017 17:00,iii,89
23,54,one,tree,56,23,7/30/2017 17:00,iii,89

Need shell command to get current date in format 7/20/2017 and then filter using the same date in a csv file from the last third column (column should not be detected from first or starting since comma varies and hence should be from last 3rd)

Answer

awk -F, -v "today=$(date "+%m/%d/%Y" | sed 's/^0//')" '$(NF-2) ~ "^"today' file

Not happy about using sed, read the date man page more closely:

awk -F, -v "today=$(date "+%-m/%d/%Y")" '$(NF-2) ~ "^"today' file
# .........................^^^

If the 3rd last column is empty or if it is “today”

awk -F, -v "today=$(date "+%-m/%d/%Y")" '$(NF-2) == "" || $(NF-2) ~ "^"today' file

Leave a Reply

Your email address will not be published. Required fields are marked *