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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed: zero-padding dates (or: convert d/m/yyyy to dd/mm/yyyy)
# 1  
Old 07-29-2009
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:

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

Code:
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
# 2  
Old 07-29-2009
Solution using awk
Code:
$ 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
$

Code:
$ 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
$

# 3  
Old 07-29-2009
try this
if SOMESTUFF,YETMORE... doesn't have any single digit number

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

else

No idea Smilie

Last edited by johnbach; 07-29-2009 at 10:42 AM..
# 4  
Old 07-30-2009
may try below perl:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date format YYYY/MM/DD to DD/MM/YYYY

I am getting output of YYYY-MM-DD and want to change this to DD/MM/YYYY. When am running the query in 'Todd' to_date(column_name,'DD/MM/YYYY') am getting the required o/p of DD/MM/YYYY, But when am executing the same query(Netezza) in linux server(bash) am getting the output of YYYY-MM-DD file... (3 Replies)
Discussion started by: Roozo
3 Replies

2. UNIX for Dummies Questions & Answers

Epoch date to YYYY/MM/DD or MM/DD/YYYY

I've seen a lot of posts on this and have tried the following: echo 1257000000| perl -e '($d,$m,$y)=(localtime(time-86400));$m+=1;$y+=1900;printf "$y/$m/$d\n";' But I am unable to convert a past Epoch date into a format such as YYYY/MM/DD or MM/DD/YYYY. I am using bash and don't know... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. Shell Programming and Scripting

Converting date DD MM YYYY to DD MON YYYY

Hello, I am writing a script that parses different logs and produces one. In the source files, the date is in DD MM YYYY HH24:MI:SS format. In the output, it should be in DD MON YYY HH24:MI:SS (ie 25 Jan 2010 16:10:10) To extract the dates, I am using shell substrings, i.e.: read line ... (4 Replies)
Discussion started by: Adamm
4 Replies

4. Shell Programming and Scripting

change date format from yyyy/mm/dd to dd/mm/yyyy

(Attention: Green PHP newbie !) I have an online inquiry form, delivering a date in the form yyyy/mm/dd to my feedback form. If the content passes several checks, the form sends an e-mail to me. All works fine. I just would like to receive the date in the form dd/mm/yyyy. I tried with some code,... (6 Replies)
Discussion started by: keyboarder
6 Replies

5. UNIX for Advanced & Expert Users

fetch dates for last 36 days in format yyyy-mm-dd

can anyone please suggest me some ideas for writing a korn shell script which will go back to 36 days from current day and print each day (incremented by 1) in the format yyyy-mm-dd until the current day. Thanks Mark (7 Replies)
Discussion started by: pavan_test
7 Replies

6. Shell Programming and Scripting

convert d/m/yyyy to YYYY-MM-DD

My csv has data like this x,x,3452,2/18/1986,abc x,g,19711,1/24/1986,abc i want to replace date in the following format YYYY-mm-dd how do i convert using awk script ? (8 Replies)
Discussion started by: nuthalapati
8 Replies

7. Shell Programming and Scripting

Date difference between 2 dates in 'yyyy-mm-dd hh:mm:ss' format

Hi all, I know this may have already been asked but hey ho... i have two dates in the 'yyyy-mm-dd hh:mm:ss' format. '2009-01-03 01:00:00' '2009-04-05 00:00:00' How can i, in shell script determine their differences? Please note, the time may not be available, so please suggest both... (4 Replies)
Discussion started by: muay_tb
4 Replies

8. Shell Programming and Scripting

Convert DD-MMM-YYYY to MM-DD-YYYY

I have a file which has 100k+ records like this abc,05-JUN-1974,def,lkj,aaa def,11-SEP-1975,ghj,dis,dea I want to convert ex 05-JUN-1974 to 06/05/1974 Please help me with awk script to convert the whole file into MM-DD-YYYY Thank you! (2 Replies)
Discussion started by: nuthalapati
2 Replies

9. Shell Programming and Scripting

converting the date field from dd/mm/yyyy to yyyy/mm/dd

How to convert the date field from dd/mm/yyyy to yyyy/mm/dd in unix my script will generate text file which have two fields one is date and another is name of the server for example this is sample date which I have to sort based on older to newer date the problem is when I found out sort will... (4 Replies)
Discussion started by: pareshan
4 Replies

10. Shell Programming and Scripting

Change Date from dd-mmm-yyyy to mm/dd/yyyy

I want to change a date from format dd-mmm-yyyy to mm/dd/yyyy. Is there a way to do this with sed or do you have to write a case statement to convert JAN to 01? Thanks (9 Replies)
Discussion started by: stringzz
9 Replies
Login or Register to Ask a Question