Solved Changing date format in UNIX

May 25, 2011 at 04:26:52
Specs: AIX
Hi,

I have a pipe delimited file with many date fields in the format of MM/DD/YY, I need to change the date to MMDDCCYY. The command I'm using is: sed 's/\(..\)\/\(..\)\/\(..\)/\1\220\3/', this command is changing more than one field. is it possible to specify the field # to change. the record look like 123456|051155|041412|081921|
I only need to change 051155 to 05111955

many thanks


See More: Changing date format in UNIX

Report •

#1
May 25, 2011 at 09:37:12
✔ Best Answer
When I have to deal with fields, I use awk. You don't specifiy how the centure is determined. I made an assumption that anything greater than 20 is in the previous century. Your use may vary:

#!/bin/ksh

# assume field 2 is MMDDYY
awk ' BEGIN { OFS=FS="|" }
{
# parse field 2
yr=substr($2,5) # get columns 5 & 6
md=substr($2,1,4) # get the first 4 columns

if(yr > 20)
   yrstr="19"yr
else
   yrstr="29"yr

# rebuild field 2
$2=md
$2=$2yrstr
print $0
} ' file.txt


Report •

#2
May 25, 2011 at 10:52:29
nails,
Sorry if my question wasn't clear, the date field is in the format of mm/dd/ccyy not mmddyy as I stated above.
my record look like: 123456|05/11/1955|04/14/1912|08/19/1921|
I only need to change the 2nd date which is 04/14/1912 to 04141912

Thank you so much for your help


Report •

#3
May 25, 2011 at 12:00:11
You want to delete the slashes in the 3rd field?

#!/bin/ksh

awk ' BEGIN { OFS=FS="|" }
{
gsub("\/", "", $3)
print $0
} ' file.txt


Report •

Related Solutions

#4
May 26, 2011 at 06:37:33
Thank you, Thank you. That's what I was looking for.

Report •

Ask Question