[SOLVED] Making mktime/strftime available to mawk

I frequently use awk time functions and am switching some scripts over to mawk. I don't have the mktime or strftime functions in mawk, but it appears that there is a way, as explained here in "Time functions":

So, simple question... How do I make support for C mktime and strftime functions available? Apologies if I've overlooked something simple, and many thanks in advance.

Well, what is your system? does it have mktime and strftime?

The first thing that comes to mind is upgrading your version of mawk.

Thanks for the reply. I'm running CentOS 6.0 x86_64. I'm very sorry to say I don't know how to determine whether it has mktime and strftime. How would I do that?

Also, while the unix.com man page library does have a mawk entry, it appears to differ from the one I linked, lacking at least the time-related entries I cited. Is it still ok to link externally in that case?

Again, thanks for the help.

You have UNIX or Linux, therefore you have mktime and strftime. These functions are fairly standard.

You may have go upgrade your version of mawk to have them in that language.

I think what I have is the newest version of mawk available for my system:

# mawk -W version
mawk 1.3.4 20120627

How might I get it to recognize the functions I need? Or, for that matter, how would I verify their availability on my system? Regular awk can use those functions. Does it call the same C functions that mawk apparently does? Thanks again for the responses.

mawk does not have a strftime function, strftime is a built in function of gawk.

that's what I'd thought as well, but the man page posted at the site where I got mawk indicated otherwise. I'd link it, but apparently that gets smooshed if I try.

I recently learned otherwise. Recent versions of mawk have borrowed this useful feature, possibly because some Linux distributions have taken to mawk as a faster substitute for GNU awk.

2 Likes

mawk-20121129 is the latest version. You can download the sources and easily build it on CentOS 6.

1 Like

From the mawk changelog:

20121129
...
        + add systime and mktime functions
...
        + integrate strtime function (patch by Dominic Letz).

Regards,
Alister

4 Likes

woot!

# mawk -W version
mawk 1.3.4 20131226
Copyright 2013, Thomas E. Dickey
Copyright 1996, Michael D. Brennan

internal regex
compiled limits:
max NF             32767
sprintf buffer      2040
# echo "hi" | mawk '{print systime()}'
1389377609
# echo "hi" | mawk '{print strftime("%D",1389377609)}'
01/10/14

Many thanks to everyone. I was certain that I had the latest version, but I must have misread.

1 Like