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 Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of 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 without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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
We are here to answer your question about 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 - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji