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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting a date within a file 2

Status
Not open for further replies.

huntb

Technical User
Jun 2, 2011
1
0
0
GB
I have script to read a file, reformat the record and output to second file. the output file has a date in julian format (YYDDD) for which i am currently using the system date. I need to be able to read the date in the orginal file and convert this. Here's my script :-

#!/bin/ksh
#
#

jul=`date +%y%j`

{ while read myline;do
# echo $myline
TY=`expr substr "$myline" 1 1`
AC=`expr substr "$myline" 325 8`
SC=`expr substr "$myline" 314 6`
CAC=`expr substr "$myline" 58 6`
CSC=`expr substr "$myline" 69 8`
CN=`expr substr "$myline" 103 18`
REF=`expr substr "$myline" 600 18`
AM=`expr substr "$myline" 44 14`
EN=`expr substr "$myline" 359 18`
DT=`expr substr "$myline" 1100 8` # Date stored in YYYYMMDD format
F1='099'
F2='0'
f3=' '

if [ $TY = 'B' ]
then
echo $DJ
echo $DT
echo "$SC""$AC""$F1""$CSC""$CAC""$F2""$AM""$CN""$REF""$EN"' '"$jul" >> /home/huntb/fsp1.txt
fi
done } < /home/huntb/fsp.txt


Any help would be much appreciated

Sorry if this has been asked before, have found similar threads but nothing i could use.
 
I find date manipulation easiest using perl combined with the POSIX mktime() function call:

Code:
DJ=$(
        /usr/bin/perl -we '
                use strict;
                use POSIX;

                my ($yyyy,$yy,$m,$d) = $ARGV[0] =~ m/(\d\d(\d\d))(\d\d)(\d\d)/;
                my (undef,undef,undef,undef,undef,undef,undef,$yday,undef) = localtime(POSIX::mktime( 0, 0, 0, $d, $m-1, $yyyy-1900 ));

                printf "%02d%03d\n",$yy,$yday+1;
        ' $DT
)

The first part splits the YYYYMMDD up into its components, retaining both the long and short form of the year.

The second part uses mktime() to calculate the Julian $yday based on the known year, month and date, with a little mathematics because mktime() uses 0-based Julian and month days, and counts years from 1900.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top