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 > Re: reformattin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 2203 of 2236
Post > Topic >>

Re: reformatting date strings

by Ed Morton <morton@[EMAIL PROTECTED] > Mar 28, 2008 at 05:08 PM

On 3/25/2008 9:03 PM, Seb wrote:
> On Wed, 26 Mar 2008 02:45:40 +0100,
> Janis Papanagnou <Janis_Papanagnou@[EMAIL PROTECTED]
> wrote:
> 
> [...]
> 
> 
>>You can write two gensub()s, where you replace the third
>>[[:digit:]]{2} subpattern by 0[[:digit:]] and [1-9][[:digit:]], resp.,
>>and add the respective century information before the replacement \\3.
> 
> 
> Good idea, this seemed to work:
> 
> 
> awk --re-interval '{
>     sub(/ end,/, " ending,")
>     if ($0 ~ /[[:digit:]]{2}\/[[:digit:]]{2}\/[[:digit:]]{2}/) {
>        
$0=gensub(/([[:digit:]]{2})\/([[:digit:]]{2})\/([1-9])([[:digit:]])/,
>             "19\\3\\4-\\1-\\2", "g")
>         $0=gensub(/([[:digit:]]{2})\/([[:digit:]]{2})\/0([[:digit:]])/,
>             "200\\3-\\1-\\2", "g")
>     }
>     print
> }'
> 
> The regexps are a bit nasty though.  Thanks!

I don't think you need the test, just do the gensub() and the pattern will
either match or it won't. Maybe you could simplify it a bit too, e.g.:

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

	Ed.




 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 Sat May 17 2:52:56 CDT 2008.