Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > reformatting da...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 2203 of 2234
Post > Topic >>

reformatting date strings

by Seb <spluque@[EMAIL PROTECTED] > Mar 26, 2008 at 01:15 AM

Hi,

I have several tens of files containing SQL code with lines of the form:

INSERT INTO tablex (var1, var2, var3, var4, var5) VALUES (1,'03/20/08
00:00:00','AM',4,NULL,'some text',NULL);

The time stamp string may consist of both date and time, or only date,
but the date is always MM/DD/YY.  I'd like to reformat the date as
YYYY-MM-DD, so the date in the example above should be "2008-03-20".
There are 2 issues I'm struggling with: 1) years may be in the 20th or
21st century, so that every year starting with 0 should belong in the
current century, and the rest in the previous, and 2) time stamps may be
present in more than one variable (i.e. the var's above).  gensub()
seemed the way to go:

awk --re-interval '{
    sub(/ end,/, " ending,")
    if ($0 ~ /[[:digit:]]{2}\/[[:digit:]]{2}\/[[:digit:]]{2}/) {
        $0=gensub(/([[:digit:]]{2})\/([[:digit:]]{2})\/([[:digit:]]{2})/,
            "\\3/\\2/\\1", "g")
    }
    print
}'

This of course only rearranges the date pieces, but doesn't add the
century information.  The sub() is just to show that other operations
are needed before printing every line.  Any suggestions on how to
proceed in this case?

Cheers,

-- 
Seb




 4 Posts in Topic:
reformatting date strings
Seb <spluque@[EMAIL PR  2008-03-26 01:15:54 
Re: reformatting date strings
Janis Papanagnou <Jani  2008-03-26 02:45:40 
Re: reformatting date strings
Seb <spluque@[EMAIL PR  2008-03-26 02:03:40 
Re: reformatting date strings
Ed Morton <morton@[EMA  2008-03-28 17:08:02 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun May 11 18:31:06 CDT 2008.