Unix Shell - string to date KSH

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > string to date KSH





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author string to date KSH
Rafael The Engel

2006-10-26, 7:16 am

Hello everyone,
I need your help

I have a string the contains a date "20061024"

I wand to convert it to 24/10/2006

Thanks

Rafael

Ed Morton

2006-10-26, 1:15 pm

Rafael The Engel wrote:
> Hello everyone,
> I need your help
>
> I have a string the contains a date "20061024"
>
> I wand to convert it to 24/10/2006


$ date="20061024"
$ tmp="${date%??}"
$ echo "${date#??????}/${tmp#????}/${tmp%??}"
24/10/2006

Regards,

Ed.
Rafael The Engel

2006-10-26, 1:15 pm

You are the MAN!!!!!!

thanks a lot

Rafael

Stephan Grein

2006-10-26, 1:15 pm

Rafael The Engel wrote:
> Hello everyone,
> I need your help
>
> I have a string the contains a date "20061024"
>
> I wand to convert it to 24/10/2006
>

Maybe you like sed. ;-)

sed
's/\([[:digit:]]\{4\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)/\3\/\2\/\1/'
<<< "20061024"

sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\3\/\2\/\1/' <<<
"20061024"


> Thanks
>
> Rafael
>


HTH,
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Stephan Grein

2006-10-26, 1:15 pm

Uhm!
<<< "string" works only on GNU Bash imho.
You can use echo "date" | sed ...

Sorry.
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Janis Papanagnou

2006-10-26, 1:15 pm

Stephan Grein wrote:
> Uhm!
> <<< "string" works only on GNU Bash imho.


....and in (newer) ksh93's.

Janis

> You can use echo "date" | sed ...
>
> Sorry.

Janis Papanagnou

2006-10-26, 1:15 pm

Janis Papanagnou wrote:
> Stephan Grein wrote:
>
>
> ...and in (newer) ksh93's.


....and in zsh.
[vbcol=seagreen]
> Janis
>
Radoulov, Dimitre

2006-10-26, 1:15 pm

> I have a string the contains a date "20061024"
>
> I wand to convert it to 24/10/2006


ksh93/bash

$ var="20061024"

$ echo "${var:0:4}/${var:4:2}/${var:6:2}"
2006/10/24


Regards
Dimitre


Radoulov, Dimitre

2006-10-26, 1:15 pm

>> I have a string the contains a date "20061024"
>
> ksh93/bash
>
> $ var="20061024"
>
> $ echo "${var:0:4}/${var:4:2}/${var:6:2}"
> 2006/10/24


Sorry

echo "${var:6:2}/${var:4:2}/${var:0:4}"
24/10/2006


Regards
Dimitre


vishu

2006-10-26, 7:19 pm

Could you please explain the following code in brief words.
Ed Morton wrote:
> Rafael The Engel wrote:
>
> $ date="20061024"
> $ tmp="${date%??}"
> $ echo "${date#??????}/${tmp#????}/${tmp%??}"
> 24/10/2006
>
> Regards,
>
> Ed.


Janis Papanagnou

2006-10-26, 7:19 pm

vishu wrote:
> Could you please explain the following code in brief words.
> Ed Morton wrote:
>

A string assignment.
[vbcol=seagreen]

Strip the last two characters "24" from string 'date', store the
remainder "200610" in 'tmp'.
[vbcol=seagreen]

Three parts separated by '/' are printed;
- strip first (#) six characters from 'date' leaving "24"
- strip first (#) four characters from 'tmp' leaving "10"
- strip the last (%) two characters from 'tmp' leaving "2006"

Janis
[vbcol=seagreen]
>
>

Michael Tosch

2006-10-26, 7:19 pm

Stephan Grein wrote:
> Rafael The Engel wrote:
> Maybe you like sed. ;-)
>
> sed
> 's/\([[:digit:]]\{4\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)/\3\/\2\/\1/'
> <<< "20061024"
>
> sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\3\/\2\/\1/' <<<
> "20061024"
>
>


If you know the format is always YYYYMMHH,
and use a # delimiter:

sed 's#\(....\)\(..\)\(..\)#\3/\2/\1#'

which becomes even good readable as:
group with \( brackets \) into 4, 2, 2 character strings,
then print \3rd, \2nd, \1st bracket with a / in between.


--
Michael Tosch @ hp : com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com