|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
You are the MAN!!!!!!
thanks a lot Rafael |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Stephan Grein wrote:
> Uhm! > <<< "string" works only on GNU Bash imho. ....and in (newer) ksh93's. Janis > You can use echo "date" | sed ... > > Sorry. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
> 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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
>> 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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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. > > |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|