user710818
user710818

Reputation: 24288

How to convert one column to date format in awk script?

cat test.txt | 
 awk 'BEGIN { FS = "\t" } ;{ print  $44=strftime("%d %b %Y",$44)  "\t" $44 }'

I have received:

 31 12 1969      2012-09-27

But I have expected:

 27 Sep 2012      2012-09-27

Where is the error? Thanks.

Upvotes: 3

Views: 7113

Answers (1)

Kent
Kent

Reputation: 195239

if you really want to convert the format with awk build-in functions, this example may help you:

kent$  echo "2012-09-27" |awk '{t=$0;gsub("-"," ",t);print strftime("%d %b %Y", mktime(t" 00 00 00"))}' 
27 Sep 2012

read the description of following functions in man page:

 mktime(datespec)
 strftime([format [, timestamp[, utc-flag]]])

edit try to fix you old command, but no input file, cannot test it...

 awk 'BEGIN {FS=OFS="\t"}{t=$44;gsub("-"," ",t); print strftime("%d %b %Y", mktime(t" 00 00 00")),$44}' test.txt

Upvotes: 5

Related Questions