Why does awk's FILENAME variable return nothing when input is a empty file? Does this means awk doesn't open that file? If doesn't open how knows it's empty, or if it's open why doesn't return filename then?

I read this post, but there not explained why below should work.

awk 'BEGINFILE{print FILENAME}1' filename

and below doesn't.

awk '{print FILENAME}' filename #or
awk 'BEGIN{print FILENAME}' filename
  • A good question. See the following and see FILENAME pagagraph: kirste.userpage.fu-berlin.de/chemnet/use/info/gawk/gawk_11.html I quote Inside a BEGIN rule, the value of FILENAME is "", since there are no input files being processed yet – Valentin Bajrami Oct 21 '17 at 13:50
  • Thanks, there I can see mentioned "FILENAME is "", since there are no input files being processed yet". With considering this apart of if it's in BEGIN or not, means FILENAME set when not in BEGIN and a input file specified not depending on "if file has a field" as below Nd64's answer points and as I tested it depends on if a file has a field but not documented. – αғsнιη Oct 21 '17 at 13:55
  • To elaborate on my previous comment. Since the BEGIN statement is ran before anything, at that moment FILENAME or any other built in variable like NR or NF is undefined/empty. – Valentin Bajrami Oct 21 '17 at 13:55
  • My suggestion: try experimenting with awk '{print "Line " NR " of " FILENAME}' test.txt, for files with different numbers of lines. How many times does it print for an empty file? – mwfearnley Oct 6 '18 at 13:07

From the awk manual:

FILENAME  A  pathname  of  the  current input file.
Inside a BEGIN action the value is undefined.  (...)

I think this is the explanation. Until a field is processed the value of FILENAME is undefined. Since no field is processed in case of an empty file the variable stays uninitialised.

| improve this answer | |
  • thanks, this "Until a field is processed the value of FILENAME is undefined" should be updated in awk's man page at least. – αғsнιη Oct 21 '17 at 14:00
  • Agree! By the way, I checked the source and could not quickly see why it should be uninitialised... Well, we have seen that apparently it is so. – Ned64 Oct 21 '17 at 17:40
  • I'm a bit confused by the last paragraph of this answer, but I don't think it's right. FILENAME is defined as soon as each file is processed (before it even knows whether it's empty or not), so it's always set in BEGINFILE. But it's not defined before then, so you wouldn't expect it to be set in BEGIN. – mwfearnley Oct 5 '18 at 23:54
  • s/field/record/. An empty file contains no records and no fields, but {print FILENAME} works fine for an empty record with no fields; it is only the absence of a record that causes the action to not be executed. – dave_thompson_085 Oct 6 '18 at 0:10
  • {print FILENAME} works fine, but it prints N times for a file with N records. There should be no confusion about how many times it prints for a file with zero records. And just to be clear, FILENAME is defined as soon as the BEGINFILE block executes, which happens before the first byte/field/record is processed, and I suspect before it attempts to open the file. – mwfearnley Oct 6 '18 at 12:50

Note: parts of this answer are specific to GNU awk, specifically 4.0 and later, which added BEGINFILE/ENDFILE

awk '{print "File name: " FILENAME}' myfile

This will print File name: myfile once for every line in myfile. If myfile is a blank file (zero bytes), it will contain no lines, and so the above string won't be printed at all.

awk 'BEGINFILE{print "File name: " FILENAME}' myfile

If supported, this will print File name: myfile once, before processing any lines. (Otherwise it will probably decide that BEGINFILE is variable with a false value, and print nothing at all.)

awk 'BEGIN{print "File name: " FILENAME}' myfile

This block is evaluated happens before the any of the files are processed, and at this time the value of FILENAME is not defined.

The gawk documentation specifically defines it as "" though, so we can know there it will just print File name:.

| improve this answer | |

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.