问题描述
Gawk 5.0.0已于2019年4月12日发布.通过公告我发现了这一点:
Gawk 5.0.0 was released on April 12, 2019. Going through the announcement I found this:
(...)11.命名空间已实现!请参阅手册.这样的结果之一是,-i
中包含的文件,-f
中读取的文件以及命令行程序段都必须是独立的语法单元.例如,您将无法再执行以下操作:
(...) 11. Namespaces have been implemented! See the manual. One consequence of this is that files included with -i
, read with -f
, and command line program segments must all be self-contained syntactic units. E.g., you can no longer do something like this:
gawk -e 'BEGIN {' -e 'print "hello" }'
我很好奇这种不再支持的行为,但是不幸的是我的Gawk 4.1.3并没有提供太多输出:
I was curious about this behaviour that is no longer supported, but unfortunately my Gawk 4.1.3 did not offer much output out of it:
$ gawk -e 'BEGIN {' -e 'print "hello" }'
gawk: cmd. line:1: BEGIN {
gawk: cmd. line:1: ^ unexpected newline or end of string
根据我在GAWK 4.2手册中所看到的,-e
选项已经被标记为有问题的:
From what I see in the manual of GAWK 4.2, the -e
option was marked as problematic already:
-e程序文本
--source
程序文本
-e program-text
--source
program-text
在程序文本中提供程序源代码.此选项使您可以将文件中的源代码与您在命令行中输入的源代码混合在一起.当您具有要在命令行程序中使用的库函数时,此功能特别有用(请参阅AWKPATH变量).
Provide program source code in the program-text. This option allows you to mix source code in files with source code that you enter on the command line. This is particularly useful when you have library functions that you want to use from your command-line programs (see AWKPATH Variable).
请注意,gawk会将每个字符串视为以换行符结尾(即使不是以换行符结尾).这样可以简化整个程序的构建.
Note that gawk treats each string as if it ended with a newline character (even if it doesn’t). This makes building the total program easier.
注意:目前,不要求每个程序文本都必须是完整的语法单元.即,以下当前有效:
CAUTION: At the moment, there is no requirement that each program-text be a full syntactic unit. I.e., the following currently works:
$ gawk -e 'BEGIN { a = 5 ;' -e 'print a }'
-| 5
但是,这种情况将来可能会改变,因此依靠此功能并不是一个好主意.
However, this could change in the future, so it’s not a good idea to rely upon this feature.
但是,这再次在我的控制台中失败:
But, again, this fails in my console:
$ gawk -e 'BEGIN {a=5; ' -e 'print a }'
gawk: cmd. line:1: BEGIN {a=5;
gawk: cmd. line:1: ^ unexpected newline or end of string
那么gawk -e 'BEGIN {' -e 'print "hello" }'
在Gawk上到底做了什么< 5?
So what is gawk -e 'BEGIN {' -e 'print "hello" }'
doing exactly on Gawk < 5?
推荐答案
它正在做您所期望的工作-将零件连接起来形成gawk 'BEGIN {print "hello" }'
,然后执行它.您实际上可以通过查看gawk如何组合代码段精美印刷它:
It's doing just what you'd expect - concatenating the parts to form gawk 'BEGIN {print "hello" }'
and then executing it. You can actually see how gawk is combining the code segments by pretty-printing it:
$ gawk -o- -e 'BEGIN {' -e 'print "hello" }'
BEGIN {
print "hello"
}
该脚本没有必要在各节中进行编写和连接,但是如果您考虑使用以下内容:
That script isn't useful to be written in sections and concatenated but if you consider something like:
$ cat usea.awk
{ a++ }
$ echo foo | gawk -e 'BEGIN{a=5}' -f usea.awk -e 'END{print a}'
6
然后您会看到预期的功能对于将某些命令行代码与文件中存储的脚本混合以运行将很有用:
then you can see the intended functionality might be useful for mixing some command-line code with scripts stored in files to run:
$ gawk -o- -e 'BEGIN{a=5}' -f usea.awk -e 'END{print a}'
BEGIN {
a = 5
}
{
a++
}
END {
print a
}
这篇关于gawk -e'BEGIN {'-e'如何打印"hello" }' 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!