Sed: zero-padding dates (or: convert d/m/yyyy to dd/mm/yyyy)

Hi all

I have some pipe-separated data in the form:

5/12/2008 00:00:00|31/1/2009 00:00:00|SOMESTUFF|OTHERSTUFF
12/31/2008 00:00:00|15/1/2009 00:00:00|MORESTUFF|REMAININGSTUFF
1/1/1023 00:00:00|16/5/2047 00:00:00|THEREST|YETMORE

I need to zero-pad the single-digit days and months, using sed.

I came up with this, but it does not handle the case where the datestamp appears in the first position in each record:

sed -e "s;\|\([1-9]\)\(\/[0-9]\{1,2\}\/[12][0-9]\{3\}\);\|0\1\2;g" \
      -e "s;\([0-9]\{1,2\}\)\/\([1-9]\/[12][0-9]\{3\}\);\1/0\2;g" $DUMMYTMP > $TMPFILE

The 'month' code (the second -e statement above) works fine; my problem is with the day padding.

I need to tell sed to process the day value (i.e. pad it with a zero) if there is only a single digit prior to the first "/" in the date string, and to ignore the padding operation if there is more than one digit there.

Put another way, I need to something like:

s;(start of record|[^0-9])\([1-9]\)\(\/[0-9]\{1,2\}\/...

How do I express the optional start of record / non-digit part?

Any help appreciated. TIA.

James

Solution using awk

$ cat f1
5/12/2008 00:00:00|31/1/2009 00:00:00|SOMESTUFF|OTHERSTUFF
12/31/2008 00:00:00|15/1/2009 00:00:00|MORESTUFF|REMAININGSTUFF
1/1/1023 00:00:00|16/5/2047 00:00:00|THEREST|YETMORE
$
$ awk -F "[/ |]" '{printf("%02d/%02d/%04d %s|%02d/%02d/%04d %s|%s|%s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10)}' f1
05/12/2008 00:00:00|31/01/2009 00:00:00|SOMESTUFF|OTHERSTUFF
12/31/2008 00:00:00|15/01/2009 00:00:00|MORESTUFF|REMAININGSTUFF
01/01/1023 00:00:00|16/05/2047 00:00:00|THEREST|YETMORE
$

try this
if SOMESTUFF,YETMORE... doesn't have any single digit number

sed -e 's/\<[0-9]\>/0&/g' file

else

No idea :confused:

may try below perl:

while(<DATA>){
	s:(?<![0-9])([0-9])(?=(/[0-9]{1,2})?/[0-9]{4}):'0'.$1:eg;
	print;
}
__DATA__
5/12/2008 00:00:00|31/1/2009 00:00:00|SOMESTUFF|OTHERSTUFF
12/31/2008 00:00:00|15/1/2009 00:00:00|MORESTUFF|REMAININGSTUFF
1/1/1023 00:00:00|16/5/2047 00:00:00|THEREST|YETMORE