date fieild in a file needs to change -linux

October 17, 2011 at 11:07:11
Specs: Windows 7
Hello,
I have cvs file as like bellow,
10,121,Jun282011
23,45,Jun192011
1002,121,Jun062011

My requirement is to change the date field

1 "mmmyyyy" if date is less than 10 e.g, Jun062011 should change to Jun2011
2 "mmm1yyy" if date between 11-20 e.g, Jun192011 should change to Jun12011
3 "mmm2yyy" if date between 21-31 e.g, Jun282011 should change to Jun22011

Is there any way to use sed or awk command?


See More: date fieild in a file needs to change -linux

Report •

#1
October 17, 2011 at 12:39:12
One way is to set awk's field seperator, FS, to a comma and parse the 3rd field - the date - into its proper chunks using the substr function:

#!/bin/ksh

awk ' BEGIN { OFS=FS="," }
{
f1=substr($3, 1, 3) # month
f2=substr($3, 4, 2) # day
f3=substr($3, 6, 4) # year

dayint=f2 + 0
if( dayint < 10)
   mystr="20"
if( dayint > 10 && dayint <= 20)
   mystr="12"
if( dayint > 20)
   mystr="22"

$3=f1 mystr f3
print $0

}' datafile.txt

I leave it to you to decide what to do with day "10"


Report •
Related Solutions


Ask Question