Day of year to dd.mm.yyyy format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Day of year to dd.mm.yyyy format
# 1  
Old 02-14-2012
Day of year to dd.mm.yyyy format

Hi,
How can I convert day of year value in format(yy,doy) to normal formatted (dd.mm.yyyy) string also all of them with awk or awk system function?

in_file.txt
---------
Code:
12,043
12,044
12,045
12,046

out_file.txt
----------
Code:
12.02.2012
13.02.2012
14.02.2012
15.02.2012


imagine command like below
Code:
awk '{print ConvertDayOfYearToDate($1)}' in_file.txt > out_file_txt

best regards.

Last edited by Franklin52; 02-15-2012 at 03:31 AM.. Reason: Please use code tags for code and data samples, thank you
kocaturk
# 2  
Old 02-14-2012
This should help you out: http:// unixhelp. ed. ac. uk/CGI/man-cgi?date
Remove the spaces for the weblink...
its the man pages for the "date" command...
# 3  
Old 02-14-2012
Try this python script:

Code:
#! /usr/bin/python

import datetime
import sys

for line in open(sys.argv[1]):
        [year,nday]=line.split(",")
        dt=datetime.timedelta(days=int(nday)-1)
        d=datetime.date(int(year)+2000,1,1)+dt
        print '%02d.%02d.%4d' % (d.day,d.month,d.year)


Code:
chmod 755 ./the-above-python-script.py
./the-above-python-script.py in_file.txt > out_file.txt

# 4  
Old 02-15-2012
Code:
perl -MPOSIX -F, -lane 'BEGIN{open O,">out_file.txt"}print O strftime("%d.%m.%Y", 0, 0, 0, 0, 0, $F[0]+100, 0, $F[1]-1, 0);END{close O}' in_file.txt

# 5  
Old 02-15-2012
I would firstly like to thank everyone who answered.
I think the missing bit of information was given. Original source file has more than one field and I'm using existing AWK script for formatted output of the source file.
I could not success for formatted output of this field with awk.
two lines of the original file and awk script are below.

Input file lines
-----------------
Code:
62658 2492 308 1 1 2423233672________ 02423233672_______ 902423169300______ 12,046,15:59:58.0 000,00:00:00 046,16:00:00
62659 9232 001 1 1 00306937961503____ 6937961503________ 02123476298_______ 12,046,15:46:59.0 046,15:47:06 046,16:00:00

Code:
awk '
 { 
  for( i = 1; i <= 5; i++ ) printf "%s ", $i
 }
 
 { 
  printf "%-18s %-18s %-18s ", substr($6,1,index($6,"_")-1), substr($7,1,index($7,"_")-1), substr($8,1,index($8,"_")-1)
 }
 
 
 { 
  YEAR = substr($9,1,2)
  DAY = substr($9,4,3)
  DOY=YEAR""DAY
  printf "%s ",system(???????????)
 }
 {
  print ""
 }
' infile.txt >outfile.txt

Output file lines
-----------------
Code:
62658 2492 308 1 1         2423233672        02423233672       902423169300 15.02.2012,15:59:58.0 ------------------- 15.02.2012,16:00:00
62659 9232 001 1 1     00306937961503         6937961503        02123476298 15.02.2012,15:46:59.0 15.02.2012,15:47:06 15.02.2012,16:00:00


Best regards.

Last edited by Franklin52; 02-16-2012 at 03:28 AM.. Reason: Please use code tags for code and data samples, thank you
kocaturk
# 6  
Old 02-19-2012
Hi all;
I solved this problem. awk script is below.
Thanks for all.

Code:
awk '
BEGIN { month=1; flag=0; theYear=2000; dayOfYear=0 }
function DoyToDate(Year, Doy)
{
 theYear = int("20"Year)
 dOfYear=Doy
 dayOfYear=int(dOfYear)
 { if ( dayOfYear > 31 ) { month += 1; dayOfYear -= 31 } else { flag = 1 } }      #january
 { if ( flag == 0 ) { if (( theYear % 4 != 0) && (dayOfYear > 28 )) { month += 1; dayOfYear -= 28 } else {
  { if (( theYear % 4 == 0 ) && (dayOfYear > 29)) { month += 1; dayOfYear -= 29 } else { flag = 1}}}}}  #february
 { if ( flag == 0 ) { if ( dayOfYear > 31 ) { month += 1; dayOfYear -= 31 } else { flag = 1 } } }   #march
 { if ( flag == 0 ) { if ( dayOfYear > 30 ) { month += 1; dayOfYear -= 30 } else { flag = 1 } } }   #april
 { if ( flag == 0 ) { if ( dayOfYear > 31 ) { month += 1; dayOfYear -= 31 } else { flag = 1 } } }   #may
 { if ( flag == 0 ) { if ( dayOfYear > 30 ) { month += 1; dayOfYear -= 30 } else { flag = 1 } } }   #june
 { if ( flag == 0 ) { if ( dayOfYear > 31 ) { month += 1; dayOfYear -= 31 } else { flag = 1 } } }   #july
 { if ( flag == 0 ) { if ( dayOfYear > 31 ) { month += 1; dayOfYear -= 31 } else { flag = 1 } } }   #august
 { if ( flag == 0 ) { if ( dayOfYear > 30 ) { month += 1; dayOfYear -= 30 } else { flag = 1 } } }   #september
 { if ( flag == 0 ) { if ( dayOfYear > 31 ) { month += 1; dayOfYear -= 31 } else { flag = 1 } } }   #october
 { if ( flag == 0 ) { if ( dayOfYear > 30 ) { month += 1; dayOfYear -= 30 } else { flag = 1 } } }   #november
 { if ( int (theYear) < 10 ) theYear="0"theYear }
 { if ( month < 10 ) month="0"month }
 { if ( dayOfYear < 10 ) dayOfYear="0"dayOfYear }
}
{ for( i = 1; i <= 5; i++ ) printf "%s ", $i }
 
{ printf "%18s %18s %18s ", substr($6,1,index($6,"_")-1), substr($7,1,index($7,"_")-1), substr($8,1,index($8,"_")-1) }
 
{ 
 DoyToDate(int(substr($9, 1, 2)),substr($9, 4, 3))
 printf "%s,%s ",dayOfYear"."month"."theYear,substr($9,8,8)
 month=1; flag=0; theYear=2000; dayOfYear=0
}
{
 if($10 == "000,00:00:00") 
 { printf "%s ","-------------------" }
 else
 {
  { 
  if(int(substr($9, 4, 3)) > int(substr($10,1,3)))
   { 
    DoyToDate(int(substr($9, 1, 2)) + 1, substr($10, 1, 3))
   }
   else
   { 
    DoyToDate(int(substr($9, 1, 2)), substr($10, 1, 3))
   }
   
  }
  
  {
   printf "%s,%s ",dayOfYear"."month"."theYear,substr($10,5,8)
   month=1; flag=0; theYear=2000; dayOfYear=0
  }
 }
}
 {
  { 
   if(int(substr($9, 4, 3)) > int(substr($11,1,3)))
   {
    DoyToDate(int(substr($9, 1, 2)) + 1, substr($11, 1, 3))
   }
   else
   { 
    DoyToDate(int(substr($9, 1, 2)), substr($11, 1, 3))
   }
  }
  {
   printf "%s,%s ",dayOfYear"."month"."theYear,substr($11,5,8)
   month=1; flag=0; theYear=2000; dayOfYear=0
  }
 }
 {
  print ""
 }
' infile.txt >outfile.txt

kocaturk
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date format YYYY/MM/DD to DD/MM/YYYY

I am getting output of YYYY-MM-DD and want to change this to DD/MM/YYYY. When am running the query in 'Todd' to_date(column_name,'DD/MM/YYYY') am getting the required o/p of DD/MM/YYYY, But when am executing the same query(Netezza) in linux server(bash) am getting the output of YYYY-MM-DD file... (3 Replies)
Discussion started by: Roozo
3 Replies

2. Shell Programming and Scripting

Pass date (YYYY-MM-DD) as parameter and get Day

Hi, I have a requirement where I have to pass Date to a script and get the day from it. Ex If parameter is 2015-09-29 The output should be Tuesday. Can you please tell me how to get that? (6 Replies)
Discussion started by: ashwin3086
6 Replies

3. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

4. Shell Programming and Scripting

Converting filenames from julian day to yyyy-mm-dd and retrieving weekly mean values

Hi, I need help to convert the filenames of my 9-year daily files (1999-2007) from a julian day to yyyy-mm-dd format. my original files are patterned likes the ones below. 1999001.txt 1999002.txt 1999003.txt 1999004.txt ... 1999365.txt desired output: 19990101.txt 19990102.txt... (3 Replies)
Discussion started by: ida1215
3 Replies

5. Shell Programming and Scripting

change date format from yyyy/mm/dd to dd/mm/yyyy

(Attention: Green PHP newbie !) I have an online inquiry form, delivering a date in the form yyyy/mm/dd to my feedback form. If the content passes several checks, the form sends an e-mail to me. All works fine. I just would like to receive the date in the form dd/mm/yyyy. I tried with some code,... (6 Replies)
Discussion started by: keyboarder
6 Replies

6. Shell Programming and Scripting

Get day of year

Hi, I wold like to know the day of year from a date in input. I know to get this from sysate with date +%j But from a date in input? :confused: Thanks (2 Replies)
Discussion started by: pinguc
2 Replies

7. AIX

Convert unix timestamp to year month day format ?

Hello, How do I convert unix timestamp value to 'normal' date format - to get year month and day values ? Looks like it's easy to do using GNU date (linux systems). But how do I do tthis on AIX ? I don't want to write C program, any ways to do that using unix shells ? thanks (1 Reply)
Discussion started by: vilius
1 Replies

8. Shell Programming and Scripting

Function to get day of week from YYYY-MM-DD date

Can't find out how to get the day of the week from a given date, anyone got a code snippet that could help please? Ta!! (4 Replies)
Discussion started by: couponmeup
4 Replies

9. Shell Programming and Scripting

Get yesterday's date in year-month-day format?

Dear All, Actually, i'm doing some reporting job and i need to pass yesterday's date in Year-Month-Day format(e.g. 2009-06-10) to another program for generating 2009-06-10 report. to get today's date, it's easy to just date '+%Y%m%d' , but no idea how can i get this kind of format for... (2 Replies)
Discussion started by: tiger2000
2 Replies
Login or Register to Ask a Question