14

The following command on my Mac (10.6) gives me an undefined function error:

$ awk 'BEGIN{now=strftime("%D", systime()); print now}'
awk: calling undefined function strftime
 source line number 1

On a Red Hat system, I get the expected result:

$ awk 'BEGIN{now=strftime("%D", systime()); print now}'
12/01/09

What's the deal here?

3 Answers 3

20

strftime is a GNU gawk extension. If you want to use strftime, download GNU gawk and install on your Mac. Otherwise, there are other tools like Perl/Python you can use. Check if you have them on your Mac

4
  • I installed gawk, mawk and nawk using MacPorts, but I still get the same error. Dec 2, 2009 at 16:45
  • sudo port install gawk I can confirm that gawk is in the correct PATH, but can't find any reference to strftime Dec 8, 2009 at 17:16
  • Correct syntax after installing gawk was gawk 'BEGIN{now=strftime("%D", systime()); print now}' Dec 9, 2009 at 3:29
  • My text book was using awk rather than gawk, so I was confused as to why it still wasn't working for me. Dec 9, 2009 at 3:30
13

Use Homebrew and then just run brew install gawk

0
4

You're relying on an extension to awk that's present in whichever variant (gawk, mawk, nawk, etc.) your Red Hat system happens to be using. See the Standard Unix Specification's awk description for what you can expect as a baseline for awk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.