views:

179

answers:

1

Hi i want to convert UNIX date to normal date (YYYY-MM-DD)

22222,0,0,0,14387
33333,0,0,0,14170
44444,0,0,0,14244
55555,0,0,0,14190
66666,0,0,0,14528
77777,0,0,0,14200
88888,0,0,0,0
99999,0,0,0,0

here 5th column represents UNIX date

i want to convert into

22222,0,0,0,2009-05-23

and similarly remaining rows

can any body help me

+5  A: 

A simple awk script ought to do it

awk -F, 'BEGIN{OFS=","} { print $1,$2,$3,$4,strftime("%Y-%m-%d",$5) }' myFile.txt

Cheers.

EDIT:
You're not using unix timestamps, but I checked your data, and it appears you're using days since the epoch, so here goes:

awk -F, 'BEGIN{OFS=","} { print $1,$2,$3,$4,strftime("%Y-%m-%d",$5*86400) }' myFile.txt
roe
@roe, That's not correct. Is it even possible to extract the year from an epoch using only a small part?
Anders
@Anders; you're right.. Well, it would've been correct, if it was actually unix timestamps, but they're not.. :P
roe
@roe, Your output is giving me `22222,0,0,0,1970-01-01`, looking at the op's question, that's a few years apart. Edit: yeah that's what I figured also.
Anders
@Anders; now it works.. :)
roe
@roe, I would give you two votes up, since you actually bothered looking into the reason why this was failing.
Anders
the above script showing syntax error.please help
@asn123; what's the error message?
roe
9842631423,0,0,0,nawk: calling undefined function strftime
@asn123; try replacing `awk` with `gawk` (which is the GNU version of awk) if you have it, it appears you're using a version of awk which doesn't have `strftime`.
roe
i am using below comenawk 'BEGIN{FS=",";OFS=","} {print $0,strftime("%Y-%m-%d",$5*86400)}'awk: syntax error near line 1awk: bailing out near line 1any function has to be defined before this commandplease help
@asn123; as I said, try using the GNU version, called `gawk`.
roe
hi i dont have gawk, is there any other way. please help
@asn123, what system are you on?
Anders
SunOS Airstore 5.10 Generic_118833-17 sun4u sparc SUNW,Sun-Fire-V440
@asn123, You have working version on Solaris now.
Anders
Solaris nawk don't have strftime() function.
ghostdog74