Sponsored Content
Top Forums Shell Programming and Scripting Day of year to dd.mm.yyyy format Post 302599947 by kocaturk on Sunday 19th of February 2012 01:34:31 PM
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
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
DtSearchValidDateString(library call)									     DtSearchValidDateString(library call)

NAME
DtSearchValidDateString -- Validate and convert a user date string SYNOPSIS
#include <Dt/Search.h> DtSrObjdate DtSearchValidDateString( char *date_string); DESCRIPTION
DtSearchValidDateString validates a DtSearch date string, as might have been entered by a user in a free form text field, and converts it into a valid DtSrObjdate. Since an invalid date string format returns a distinctive DtSrObjdate, this function may also be used as a boolean test for string valid- ity. ARGUMENTS
date_string Specifies the date string to be validated and converted. The format for a valid date string is "[yyyy [mm [dd]]", one, two, or three ASCII numeric tokens separated by one or more nonnu- meric chars (whitespace, slashes, etc). The integer yyyy represents a Gregorian calendar year number in the range 1990 to 5995 inclusive. If yyyy is less than 1900, 1900 is added to it. The integer mm is a Gregorian calendar month number in the range 1 to 12 inclusive. The integer dd is a Gregorian calendar day number in the range 1 to 31 inclusive. If only two tokens are in the string, they are presumed to be yyyy and mm, and dd is presumed to be to 1. If only one token is in the string, it is presumed to be yyyy, and both mm and dd are presumed to be 1. RETURN VALUE
DtSearchValidDateString returns zero (a valid DtSrObjdate) if date_string is NULL or empty. It returns a correctly formatted DtSrObjdate on successful parse and conversion. It returns -1 and a message on the MessageList if date_string is invalid. SEE ALSO
DtSrAPI(3), DtSearchQuery(3), dtsrfzkfiles(4), DtSearch(5) DtSearchValidDateString(library call)
All times are GMT -4. The time now is 08:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy