I sometimes use the technique of passing in data via the command line in
the place normally reserved for filename args. Yes, I realize this is non-standard, and that there are other ways to do it. I'm not interested
in any arguments or suggestions about those alternatives. The technique is something like:
$ someCommand | gawk 'BEGIN { ARGC = 1 }
/something/ { for (i in ARGV) print i,ARGV[i] }' 'string 1' 'string 2' ...
The trick here is that you explicitly set ARGC to 1, so that your strings don't get interpreted as filenames. Written as above, it all works fine.
As long as you "kill" ARGV via setting ARGC in the BEGIN clause, it works
as expected.
Now, just for fun, I was playing around with some alternatives, and found that neither of the following variations work (and by "not work", I mean
that it tries to interpret "string 1" as a filename, which of course fails and causes a fatal error abort from the program.
1) $ someCommand | gawk -v ARGC=1 '
/something/ { for (i in ARGV) print i,ARGV[i] }' 'string 1' 'string 2' ...
2) $ someCommand | gawk '
/something/ { for (i in ARGV) print i,ARGV[i] }' ARGC=1 'string 1' 'string 2' ...
I'm curious as to why neither of these work. To my mind, it seems they should.
(Particularly, the first one; I can sort of get why the second one might
not work)
On 05.02.2021 02:57, Kenny McCormack wrote:
I sometimes use the technique of passing in data via the command line in
the place normally reserved for filename args. Yes, I realize this is
non-standard, and that there are other ways to do it. I'm not interested
in any arguments or suggestions about those alternatives. The technique is >> something like:
$ someCommand | gawk 'BEGIN { ARGC = 1 }
/something/ { for (i in ARGV) print i,ARGV[i] }' 'string 1' 'string 2' ...
The trick here is that you explicitly set ARGC to 1, so that your strings
don't get interpreted as filenames. Written as above, it all works fine.
As long as you "kill" ARGV via setting ARGC in the BEGIN clause, it works
as expected.
Now, just for fun, I was playing around with some alternatives, and found
that neither of the following variations work (and by "not work", I mean
that it tries to interpret "string 1" as a filename, which of course fails >> and causes a fatal error abort from the program.
1) $ someCommand | gawk -v ARGC=1 '
/something/ { for (i in ARGV) print i,ARGV[i] }' 'string 1' 'string 2' ...
2) $ someCommand | gawk '
/something/ { for (i in ARGV) print i,ARGV[i] }' ARGC=1 'string 1' 'string 2' ...
I'm curious as to why neither of these work. To my mind, it seems they should.
(Particularly, the first one; I can sort of get why the second one might
not work)
I wouldn't expect anything here. While the three variants seem to do
the same they are obviously and effectively triggered at different
"instances of time". Because of that I get different error messages
for the two error cases. So it depends on when the file-open command
is issued and when it is determined whether files are present or not.
Has the GNU Awk manual nothing to say about the processing order?
Does POSIX specify anything about it?
In article <26uveh-7cm.ln1@ID-313840.user.individual.net>,
Geoff Clare <netnews@gclare.org.uk> wrote:
Has the GNU Awk manual nothing to say about the processing order?
Does POSIX specify anything about it?
There are some clarifications about ARGC and ARGV planned for the
next revision of POSIX. See:
https://austingroupbugs.net/view.php?id=974#c3231
One of the things the new description says is "It is unspecified
whether alterations to ARGC can be made using the -v option."
However, for the second command, unless I missed something I think it
is (will be) required to work.
Thanks for this link and info. I will be reviewing what it says
and if necessary I will fix gawk to take this into account.
Brian Kernighan's awk "correctly" handles the case where ARGC=1 appears
in place of a filename. Mawk and gawk don't. I haven't yet tried
any other awks.
Has the GNU Awk manual nothing to say about the processing order?
Does POSIX specify anything about it?
There are some clarifications about ARGC and ARGV planned for the
next revision of POSIX. See:
https://austingroupbugs.net/view.php?id=974#c3231
One of the things the new description says is "It is unspecified
whether alterations to ARGC can be made using the -v option."
However, for the second command, unless I missed something I think it
is (will be) required to work.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 285 |
Nodes: | 16 (2 / 14) |
Uptime: | 76:09:13 |
Calls: | 6,489 |
Calls today: | 2 |
Files: | 12,096 |
Messages: | 5,276,210 |