0

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)

4
  • using command DATE=date +%m/%d/%Y got today's in correct format as 10/25/2017 Oct 24, 2017 at 19:04
  • Need only to filter DATE variable from last 3rd column of Input csv file Oct 24, 2017 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 Oct 24, 2017 at 19:27
  • Added a requirement in a question Oct 24, 2017 at 20:41

2 Answers 2

1
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
6
  • Do you understand how it works? Oct 24, 2017 at 19:41
  • yeah..today's date format is stored in today variable and then NR from last 3 column filters the date. Oct 24, 2017 at 19:56
  • if from the last 3rd column is empty then it can redirected to another file ? Oct 24, 2017 at 20:00
  • Please update your requirements in your question Oct 24, 2017 at 20:14
  • not able to update the question Oct 24, 2017 at 20:21
1

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)
5
  • awk -F',' 'BEGIN{ d=strftime("%m/%d/%Y", systime()) }$(NF-2) ~ d' file command only print lines if last 3rd column is empty ? Oct 24, 2017 at 20:56
  • @user8554534, now you have a complete solution, see my update Oct 24, 2017 at 20:59
  • only recommendation: use "%-m to strip the leading zero from the month. Oct 24, 2017 at 21:01
  • @glennjackman, you have my update Oct 24, 2017 at 21:07
  • and you have my vote ;) Oct 24, 2017 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .