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)

  • using command DATE=date +%m/%d/%Y got today's in correct format as 10/25/2017 – user8554534 Oct 24 '17 at 19:04
  • Need only to filter DATE variable from last 3rd column of Input csv file – user8554534 Oct 24 '17 at 19:04
  • 1
    I'd strongly recommend you start using ISO8601 date format: YYYY-MM-DD. It's completely unambiguous, plus it sorts the same lexically and chronologically. Some systems provide date +%F as a shorthand for date +%Y-%m-%d – glenn jackman Oct 24 '17 at 19:27
  • Added a requirement in a question – user8554534 Oct 24 '17 at 20:41
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
  • Do you understand how it works? – glenn jackman Oct 24 '17 at 19:41
  • yeah..today's date format is stored in today variable and then NR from last 3 column filters the date. – user8554534 Oct 24 '17 at 19:56
  • if from the last 3rd column is empty then it can redirected to another file ? – user8554534 Oct 24 '17 at 20:00
  • Please update your requirements in your question – glenn jackman Oct 24 '17 at 20:14
  • not able to update the question – user8554534 Oct 24 '17 at 20:21

With single GNU awk process:

awk -F',' 'BEGIN{ d=strftime("%-m/%d/%Y", systime()) }
          $(NF-2)~/^[[:space:]]*$/{ print > "empty_dates.txt" }$(NF-2)~d' file
  • $(NF-2)~/^[[:space:]]*$/{ print > "empty_dates.txt" } - if the 3rd field (at the end) is empty - print the line into file empty_dates.txt (you may specify the filename which you like)
  • awk -F',' 'BEGIN{ d=strftime("%m/%d/%Y", systime()) }$(NF-2) ~ d' file command only print lines if last 3rd column is empty ? – user8554534 Oct 24 '17 at 20:56
  • @user8554534, now you have a complete solution, see my update – RomanPerekhrest Oct 24 '17 at 20:59
  • only recommendation: use "%-m to strip the leading zero from the month. – glenn jackman Oct 24 '17 at 21:01
  • @glennjackman, you have my update – RomanPerekhrest Oct 24 '17 at 21:07
  • and you have my vote ;) – glenn jackman Oct 24 '17 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.