"invalid date" error message when converting date format from csv file ? -shell

Viewed 341

I am new to shell scripting, I am trying to get the days of week from csv column that has dates in format :

16/10/2015
19/06/2010
16/09/2013
4/7/2008
29/12/2017

I used date -%A and got the results converted but not to all and still getting error message with results :

date: invalid date ‘16/10/2015’ 
date: invalid date ‘19/06/2010’ 
date: invalid date ‘16/09/2013’ 
Monday 
date: invalid date ‘29/12/2017’ 
Monday

Expected output:

Friday
Saturday
Monday
Wednesday
Friday

my code :

cat file.csv | awk -F "," 'NR>1 {print $4}' | awk -F, '{"date +%A -d\""$1 "\""| getline dte;$1=dte}1' OFS=","

I also tried to reformat the values into date type before converting to make sure but still getting the error message, I have a big file of data that's why I am trying to loop on the whole column in awk

2 Answers

Could you please try following in pure bash(tested and written with provided samples only).

while read line
do
  IFS='/' read -ra arr <<< "$line"
  newDate=${arr[2]}/${arr[1]}/${arr[0]}
  date -d  $newDate +%A
done < "Input_file"

For provided samples following will be the output.

Friday
Saturday
Monday
Friday
Friday

Convert date fast to full name of the day of the week with GNU awk:

awk 'BEGIN{FS="/"} {t=sprintf("%d %d %d 0 0 0",$3,$2,$1); print strftime("%A",mktime(t))}' Input_file

See: gawk - Date and time calculation functions

Related