Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

week date conversion 1

Status
Not open for further replies.

nabana2

Technical User
Sep 26, 2005
21
ZA
Hi

I'm trying to process a file with dates in the format of "week.year".
I need to convert this to "first day of week.month.year"

example:
01.2006 to 02.01.2006
02.2006 to 09.01.2006
03.2006 to 16.01.2006

I'm not sure if it's possible to pass the date command a string with the week in it.

Is there a way to do this?

Thanks
 
I have a conversion script, based on the unix cal command and awk (weeks start on sunday). Not really optimized for speed, but if you're interested, here goes:

Code:
#!/bin/ksh
# # # # #
# convert year week to year/month/day
# # #
# variables
# # #
export year=$1
export week=$2
# # #
# get calendar for 12 months of ${year}
# # #
export LANG=En_US
for i in 1 2 3 4 5 6 7 8 9 10 11 12
do
 cal ${i} ${year}
done|\
 awk 'BEGIN {
 year=ENVIRON["year"]
 week=ENVIRON["week"]
 wk=0
}
{
 if ($2 == year) {
  smonth=$1
  if (smonth ~ /^Jan/) month=1
  if (smonth ~ /^Feb/) month=2
  if (smonth ~ /^Mar/) month=3
  if (smonth ~ /^Apr/) month=4
  if (smonth ~ /^May/) month=5
  if (smonth ~ /^Jun/) month=6
  if (smonth ~ /^Jul/) month=7
  if (smonth ~ /^Aug/) month=8
  if (smonth ~ /^Sep/) month=9
  if (smonth ~ /^Oct/) month=10
  if (smonth ~ /^Nov/) month=11
  if (smonth ~ /^Dec/) month=12
 }
 else {
  # count week numbers 
  # only if first position (Sunday) is not empty
  if (($0 !~ /^  /) && ($0 != "") && ($1 != "Sun")) {
   wk++
   if (wk == week) {
    # week number found
    printf "%04d/%02d/%02d\n", year, month, $1
   }
  }
 }
}'

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top