Reputation: 58381
I have the string:
20120113083000Z
and I want to convert it to:
2012 01 13 08 30 00 Z
ready for the mktime
command to convert it to a date stamp.
I have at present:
echo '20120113083000Z' |
awk '{print mktime(gensub(/(....)(..)(..)(..)(..)(..)(.)/,"\\1 \\2 \\3 \\4 \\5 \\6 \\7",1g,$1))}'
1326439800
I know I could use sed or bash to convert it, but I would like to keep it inside an awk process.
Is there a better (shorter or more elegant) way?
N.B. I would like to make it as generic as possible (gensub
gawk only(?)).
Upvotes: 2
Views: 1206
Reputation: 2292
echo 20120113083000Z | awk 'BEGIN { FS = "" } ; { print $1$2$3$4, $5$6, $7$8, $9$10, $11$12, $13$14, $15 }'
Upvotes: 4
Reputation: 58381
This might work for me:
echo '20120113083000Z' | awk '{gsub(/../,"& ");sub(/ /,"")}1'
2012 01 13 08 30 00 Z
Upvotes: 3
Reputation: 161654
$ echo 20120113083000 | sed 's/../& /g; s/ //' | awk '{print mktime($0)}'
$ echo 20120113083000 | awk '{print mktime(gensub(/ /, "", "1", gensub(/../, "& ", "g")))}'
Upvotes: 3