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!

Date Parsing

Status
Not open for further replies.

moonoo

Programmer
May 17, 2000
62
US
Hi I have a shell program which takes date as a argument in the format YYYYMMDD . So i have $1 as 20031217 inside my shell Program . How do i parse this date to Year , Month and Day variables ..Someone suggested to use awk ,,but not sure how to use it..
Regards
dev
 
something like that with 'awk':

eval echo "20031221" | awk '{ print "myY=" substr($0,1,4), "myM=" substr($0,5,2), "myD=" substr($0,7,2)}'

set | grep my


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You can also use cut:
Code:
Y=`echo $1|cut -c-4`
M=`echo $1|cut -c5-6`
D=`echo $1|cut -c7-`
 
Another solution with sed :

echo &quot;$1&quot; | sed -e 's/\(....\)\(..\)\(..\)/\1 \2 \3/' | read Y M D


Jean Pierre.
 
Yet another solution, with sed:
eval echo $1 | sed 's!^\(....\)\(..\)\(..\)$!Y=\1;M=\2;D=\3!'
echo &quot;Date=$1, Year=$Y, Month=$M, Day=$D&quot;

Hope This Help
PH.
 
Or with expr...

Year=`expr substr $1 1 4`
Month=`expr substr $1 5 2`
Day=`expr substr $1 7 2`
 
another solution, assuming use of gnu's date:

date -d ${1} +&quot;your_format_specification&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top