The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Conversion of .zip to .tar.Z format eagercyber Shell Programming and Scripting 2 03-26-2008 05:11 AM
conversion of unix time format sari Windows & DOS: Issues & Discussions 2 03-02-2008 11:33 PM
DateTime Format Conversion in a File srikanthgr1 Shell Programming and Scripting 4 10-23-2007 09:58 AM
convert mmddyy date format to ccyyddd format?? Bhups Shell Programming and Scripting 2 09-27-2006 08:30 PM
change the empty function from the old format to the new format powah Shell Programming and Scripting 0 06-23-2005 09:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-30-2006
Registered User
 

Join Date: Aug 2006
Location: Lyon France
Posts: 12
Date format conversion function

Hello,

does somebody knows about a function that would convert a date like:

YYMMDD into a date like YYYY-MM-DD ?

Thank you for your ideas

Reply With Quote
Forum Sponsor
  #2  
Old 08-30-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
yes, one of the 'functions' is 'sed'
Reply With Quote
  #3  
Old 08-30-2006
Registered User
 

Join Date: Aug 2006
Location: Lyon France
Posts: 12
date format conversion

I can have either :

070829 --> I want 2007-08-29
890829 --> 1989-08-29
Reply With Quote
  #4  
Old 08-30-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by Cecile
I can have either :

070829 --> I want 2007-08-29
890829 --> 1989-08-29
ok, the 'function' 'sed' should be able to do it for you.
Reply With Quote
  #5  
Old 08-30-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
The sed string to do that is worse than writing a shell function to do it IMO -
Code:
#!/bin/ksh
format ()
{
        yr=`expr substr $1 1 2`      
        month=`expr substr $1 5 2`
        day=`expr substr $1 7 2`
        if [[ $yr > "30" ]] ; then
             echo "19""$yr-$month-$day"
        else
             echo "20"$yr-$month-$day"
        fi
}

new_date=$(format "041012")
echo $new_date
Edit per vgersh99 observation.

Last edited by jim mcnamara; 08-30-2006 at 12:48 PM.
Reply With Quote
  #6  
Old 08-30-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Jim,
that was not the desired input format.

given a sample input file 'mySampleFile.txt':
Code:
070829
890829
050829
Code:
sed 's/\([^0].\)\(..\)\(..\)/19\1-\2-\3/g;s/\(0.\)\(..\)\(..\)/20\1-\2-\3/g' mySampleFile.txt
Reply With Quote
  #7  
Old 08-30-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
Quote:
sed 's/\([^0].\)\(..\)\(..\)/19\1-\2-\3/g;s/\(0.\)\(..\)\(..\)/20\1-\2-\3/g' mySampleFile.txt
Script will fail on where year is 90, 80, 70 and so on:
Code:
echo "801229" | sed 's/\([^0].\)\(..\)\(..\)/19\1-\2-\3/g;s/\(0.\)\(..\)\(..\)/20\1-\2-\3/g'
198200--12--29
I think it should be:
Code:
sed 's/\([^0].\)\(..\)\(..\)/19\1-\2-\3/g;s/^\(0.\)\(..\)\(..\)/20\1-\2-\3/g' mySampleFile.txt
Regards,
Tayyab
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0