awk: Built-in FILENAME variable on empty file
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
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
awk
add a comment |Â
up vote
4
down vote
favorite
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
awk
A good question. See the following and seeFILENAME
pagagraph: kirste.userpage.fu-berlin.de/chemnet/use/info/gawk/gawk_11.html I quoteInside 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 theBEGIN
statement is ran before anything, at that momentFILENAME
or any other built in variable likeNR
orNF
is undefined/empty.
â Valentin B
Oct 21 '17 at 13:55
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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
awk
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
awk
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 seeFILENAME
pagagraph: kirste.userpage.fu-berlin.de/chemnet/use/info/gawk/gawk_11.html I quoteInside 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 theBEGIN
statement is ran before anything, at that momentFILENAME
or any other built in variable likeNR
orNF
is undefined/empty.
â Valentin B
Oct 21 '17 at 13:55
add a comment |Â
A good question. See the following and seeFILENAME
pagagraph: kirste.userpage.fu-berlin.de/chemnet/use/info/gawk/gawk_11.html I quoteInside 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 theBEGIN
statement is ran before anything, at that momentFILENAME
or any other built in variable likeNR
orNF
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
add a comment |Â
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.
thanks, this "Until a field is processed the value of FILENAME is undefined" should be updated inawk
'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
add a comment |Â
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.
thanks, this "Until a field is processed the value of FILENAME is undefined" should be updated inawk
'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
add a comment |Â
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.
thanks, this "Until a field is processed the value of FILENAME is undefined" should be updated inawk
'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
add a comment |Â
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.
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.
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 inawk
'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
add a comment |Â
thanks, this "Until a field is processed the value of FILENAME is undefined" should be updated inawk
'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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
A good question. See the following and see
FILENAME
pagagraph: kirste.userpage.fu-berlin.de/chemnet/use/info/gawk/gawk_11.html I quoteInside 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 momentFILENAME
or any other built in variable likeNR
orNF
is undefined/empty.â Valentin B
Oct 21 '17 at 13:55