Find week of the year for given date using date command inside awk

Hi all,

Need an urgent help on the below scenario.

script:

awk -F"," 
'BEGIN { #some variable assignment}

{ #some calculation and put values in array}

END {
   year=#getting it from array and assume this will be 2014
   month=#getting it from array and this will be 05
   date=# getting it from array and this will be 20

   rt_date=year"-"month"-"date (this will be 2014-05-20)

I need to find the week of the above date.

In normal case, using date command we can find the week easily.
e.g.

date -d "$rt_date" +%-V

but this is not properly working inside the awk command :(.

I tried to use like below inside the awk.

week=$(date -d "rt_date" +%-V)

but no luck, am getting the below error.

awk: cmd. line:75:                              rt_week=$(date -d "rt_date" +%-V)
awk: cmd. line:75:                                                           ^ syntax error

I tried using

week=system(date -d "rt_date" +%-V)

but am getting the same error :(.

Please help on this.

Try

awk -v woy="$(date -d "$rt_date" +%-V)" -F"," 
'BEGIN { #some variable assignment}

{ #some calculation and put values in array}

END {
   year=#getting it from array and assume this will be 2014
   month=#getting it from array and this will be 05
   date=# getting it from array and this will be 20

   rt_date=year"-"month"-"woy (this will be 2014-05-20)
1 Like

Hi Clx,

thanks for your reply.. I think my explanation might have given misunderstanding on the requirement.

let me explain again here.

I am getting the date string inside awk command as 2014-05-20
I need to get the week for this date.

assigning a global variable in awk is not giving me the correct week count for this date.

background:
> I am passing a csv file to this awk command. For each line there would be different date (it may not be the current date or same date).
> So using the date command or any in-build function of awk, i need to get the week for that date.

so can anyone kindly help on this... ???

Please let me know if you need any additional information.

You can use the mktime() and strftime() routines if your awk has them (gawk does). Here's a crude sample to give you an idea how it work. Check the "man gawk" page for more info

gawk 'BEGIN { dinfo = mktime("2014 05 20 12 00 00"); wno = strftime("%-V", dinfo); print wno; }'
1 Like

Ok, sorry for misunderstanding. In that case, even if it works, system returns the exit status for the command. Its not preferred to store the output.

cnamejj's solution is off course preferable, in case you dont have those built-ins,

awk ' BEGIN { 
                rt_date="2014-04-20"
                cmd="date -d "rt_date" +%-V"
                cmd | getline week
                print week
}'  
2 Likes

Hi cnamejj,

Thanks for you help.
It helped a lot but still need a small help. I need to pass the year, month and date awk variable to mktime and get the week for that date.

the below code is always returning 1 if we are passing the variables in any case.
can you help on this.

[root@vijrajam-172-20-115-37-unicorn bin]# gawk 'BEGIN { dinfo = mktime("2014 05 20 12 00 00"); wno = strftime("%-V", dinfo); print wno; }'
21
[root@vijrajam-172-20-115-37-unicorn bin]# gawk 'BEGIN {year=2014;month=05;day=20; dinfo = mktime("year month day 12 00 00"); wno = strftime("%-V", dinfo); print wno; }'
1
[root@vijrajam-172-20-115-37-unicorn bin]# gawk 'BEGIN {year=2014;month=05;day=20; dinfo = mktime("$year $month $day 12 00 00"); wno = strftime("%-V", dinfo); print wno; }'
1

Quoting variables wont interpolate them. They would be treated like literal string.

Try

dinfo = mktime(year FS month FS day" 12 00 00")
1 Like

Hi Clx,

Perfect, worked seemlessly... thanks a lot...:):b:

Vijai

---------- Post updated at 04:17 AM ---------- Previous update was at 04:12 AM ----------

Hi cnamejj,

Fantastic.... It worked very well...

[root@vijrajam-172-20-115-37-unicorn bin]# gawk 'BEGIN {year=2014;month=05;day=20; dinfo = mktime(year FS month FS day " 12 00 00"); wno = strftime("%-V", dinfo); print wno; }'
21

one final question.. which is the best/optimized way to use whether its ur solution or clx's solution...??