PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > string to date KSH
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

string to date KSH

Réponse
 
LinkBack Outils de la discussion
Vieux 26/10/2006, 11h22   #1
Rafael The Engel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut string to date KSH

Hello everyone,
I need your

I have a string the contains a date "20061024"

I wand to convert it to 24/10/2006

Thanks

Rafael

  Réponse avec citation
Vieux 26/10/2006, 13h17   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

Rafael The Engel wrote:
> Hello everyone,
> I need your
>
> 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.
  Réponse avec citation
Vieux 26/10/2006, 14h06   #3
Rafael The Engel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

You are the MAN!!!!!!

thanks a lot

Rafael

  Réponse avec citation
Vieux 26/10/2006, 16h14   #4
Stephan Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

Rafael The Engel wrote:
> Hello everyone,
> I need your
>
> 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
  Réponse avec citation
Vieux 26/10/2006, 16h16   #5
Stephan Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

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
  Réponse avec citation
Vieux 26/10/2006, 16h37   #6
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

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


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

Janis

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

  Réponse avec citation
Vieux 26/10/2006, 16h38   #7
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

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

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


....and in zsh.

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

  Réponse avec citation
Vieux 26/10/2006, 16h42   #8
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

> 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


  Réponse avec citation
Vieux 26/10/2006, 16h45   #9
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

>> 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


Sorry

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


Regards
Dimitre


  Réponse avec citation
Vieux 26/10/2006, 19h33   #10
vishu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

Could you please explain the following code in brief words.
Ed Morton wrote:
> Rafael The Engel wrote:
> > Hello everyone,
> > I need your
> >
> > 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.


  Réponse avec citation
Vieux 26/10/2006, 21h17   #11
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

vishu wrote:
> Could you please explain the following code in brief words.
> Ed Morton wrote:
>
>>Rafael The Engel wrote:
>>
>>>Hello everyone,
>>>I need your
>>>
>>>I have a string the contains a date "20061024"
>>>
>>>I wand to convert it to 24/10/2006

>>
>>$ date="20061024"


A string assignment.

>>$ tmp="${date%??}"


Strip the last two characters "24" from string 'date', store the
remainder "200610" in 'tmp'.

>>$ echo "${date#??????}/${tmp#????}/${tmp%??}"


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

>>24/10/2006
>>
>>Regards,
>>
>> Ed.

>
>

  Réponse avec citation
Vieux 26/10/2006, 21h36   #12
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to date KSH

Stephan Grein wrote:
> Rafael The Engel wrote:
>> Hello everyone,
>> I need your
>>
>> 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"
>
>


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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h27.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17835 seconds with 20 queries