awk: Built-in FILENAME variable on empty file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












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 'BEGINFILEprint FILENAME1' filename


and below doesn't.



awk 'print FILENAME' filename #or
awk 'BEGINprint FILENAME' filename






share|improve this question






















  • 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 B
    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 B
    Oct 21 '17 at 13:55














up vote
4
down vote

favorite
1












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 'BEGINFILEprint FILENAME1' filename


and below doesn't.



awk 'print FILENAME' filename #or
awk 'BEGINprint FILENAME' filename






share|improve this question






















  • 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 B
    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 B
    Oct 21 '17 at 13:55












up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





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 'BEGINFILEprint FILENAME1' filename


and below doesn't.



awk 'print FILENAME' filename #or
awk 'BEGINprint FILENAME' filename






share|improve this question














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 'BEGINFILEprint FILENAME1' filename


and below doesn't.



awk 'print FILENAME' filename #or
awk 'BEGINprint FILENAME' filename








share|improve this question













share|improve this question




share|improve this question








edited Oct 21 '17 at 13:36

























asked Oct 21 '17 at 13:30









αғsнιη

15.6k92563




15.6k92563











  • 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 B
    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 B
    Oct 21 '17 at 13:55
















  • 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 B
    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 B
    Oct 21 '17 at 13:55















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 B
Oct 21 '17 at 13:50





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 B
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





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 B
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 B
Oct 21 '17 at 13:55










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










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.






share|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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399545%2fawk-built-in-filename-variable-on-empty-file%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










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.






share|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














up vote
5
down vote



accepted










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.






share|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












up vote
5
down vote



accepted







up vote
5
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 21 '17 at 13:36









Ned64

2,44911035




2,44911035











  • 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
















  • 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















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




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




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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399545%2fawk-built-in-filename-variable-on-empty-file%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?