1835088 Members
1980 Online
110073 Solutions
New Discussion

Re: Shell Scripting..

 
Sejal Joshi
Occasional Contributor

Shell Scripting..

How I can convert 234 to mmddyy format??

date +%j
234

234 is 22nd Aug....How I can convert that and display in 082201 format.....I know we can do date +%m%d%y to get that format..But my script is giving output 234 and I want to convert that to mmddyy format??
How to analyze HPMC Chassis codes???
2 REPLIES 2
Sridhar Bhaskarla
Honored Contributor

Re: Shell Scripting..

I am not sure if date command will allow us to convert the string back to your Aug 22 format. But there is a crude way of doing it using shell script. YOu need to make a table like this

DEC:334
NOV:304
OCT:273
....
FEB:31
JAN:0

let's call it daytable

Let's store your string in a variable $STR

This program will convert your string to month and day

for i in `cat daytable`
do
MON=`echo $i|awk '{FS=":";print $1}'`
DAY=`echo $i|awk '{FS=":";print $2}'`
if [ $STR -ge $DAY ]
then
echo STR is $STR Day is $DAY
MYDAYS=`echo $STR - $DAY |bc`
echo "$MON - $MYDAYS"
exit
fi
done


This will work only for this year. You need to change the values if you want for other years

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Robin Wakefield
Honored Contributor

Re: Shell Scripting..

Hi,

Here's a perl script to do it, uses 2 arguments, day-of-year and year (default - this year), e.g.

# doy2time.pl 40 1983
09-02-1983
# doy2time.pl 269
26-09-2001

========================================
#!/opt/perl5/bin/perl

if ( $#ARGV eq 0 ) {
$year=1900+(localtime)[5];
}
else {
$year=$ARGV[1];
}
$doy=$ARGV[0];
$numdays=-1;
for ( $y=1970 ; $y < $year ; $y++ ) {
$numdays+=365;
$rem=$y%4;
$numdays+=1 if ( $rem == 0 ) ;
}
$numdays+=$doy;
$numdays*=24*3600;
($d,$m)=(localtime($numdays))[3,4];
printf ("%02d-%02d-%04d\n",$d,$m+1,$year);
===========================================

Rgds, Robin.