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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find week of the year for given date using date command inside awk
# 1  
Old 05-20-2014
Find week of the year for given date using date command inside awk

Hi all,

Need an urgent help on the below scenario.

script:
Code:
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.
Code:
date -d "$rt_date" +%-V

but this is not properly working inside the awk command Smilie.

I tried to use like below inside the awk.

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

but no luck, am getting the below error.

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

I tried using
Code:
week=system(date -d "rt_date" +%-V)

but am getting the same error Smilie.

Please help on this.
# 2  
Old 05-20-2014
Try
Code:
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)

This User Gave Thanks to clx For This Post:
# 3  
Old 05-20-2014
Quote:
Originally Posted by clx
Try
Code:
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)

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.

Last edited by vijaidhas; 05-20-2014 at 05:22 AM..
# 4  
Old 05-20-2014
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

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

This User Gave Thanks to cnamejj For This Post:
# 5  
Old 05-20-2014
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,

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

These 2 Users Gave Thanks to clx For This Post:
# 6  
Old 05-20-2014
Quote:
Originally Posted by cnamejj
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

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

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.

Code:
[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

# 7  
Old 05-20-2014
Quote:
Code:
mktime("year month day 12 00 00");

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

Try
Code:
dinfo = mktime(year FS month FS day" 12 00 00")

This User Gave Thanks to clx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg ... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

3. Shell Programming and Scripting

Help in using date command inside awk

Hi All, bash-3.2$ autorep -J BOX_NAME% -l0 | grep BOX_NAME| awk -f awkScript.awk sh: -c: line 0: unexpected EOF while looking for matching `"' sh: -c: line 1: syntax error: unexpected end of file BOX_NAME SU 06/21/2013 03:44:03 06/21/2013 07:46:37 0 #My awkfile { ... (3 Replies)
Discussion started by: ddspark
3 Replies

4. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

5. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

6. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies
Login or Register to Ask a Question