How can I get file attribute with awk ?


Is there an equivalent to c/php stat() function ?

  • obviously when awk'ing a pipe, file attribute would be undefined.
  • I know I can use perl, but this would rewrite my awk's script.
  • this in on ubuntu, any alternative to awk is OK (gawk for instance).

my awk is already gawk

awk --version
GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.

adding @load "filefuncs" ; (be it in BEGIN { } clause or before it.)

result in

gawk: stat-to-php.awk:4:     @load "filefuncs" ;
gawk: stat-to-php.awk:4:      ^ syntax error
  • You may want to write extension for gawk gnu.org/software/gawk/manual/html_node/… – cuonglm Jun 8 '16 at 7:17
  • well, thanks for the hint, this looks slightly less hellish than writing PHP extension ... – Archemar Jun 8 '16 at 7:22
  • @cuonglm - there's no need to write it, it already exists for GNU awk. enable with @load "filefuncs" at the start of the awk script. Installed as /usr/lib/x86_64-linux-gnu/gawk/filefuncs.so on my debian sid system (gawk package version 1:4.1.3+dfsg-0.1) – cas Jun 8 '16 at 10:00
  • @cas: Ah right, it's /usr/lib/gawk/filefuncs.so in my Manjaro. – cuonglm Jun 8 '16 at 10:07

If your system has a stat command, you can let awk call that command and read its output via getline.


awk '{ "stat -c%y " $1 | getline ts ;}'

will put in ts value of stat -c%y $1

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.