如果字段变量不存在,awk 是否打印所有内容?
我试图理解我继承的一些脚本并使用 awk。其中一个脚本中有以下几行:
report=`<make call to Java class that generates a report`
report=`echo $report|awk '{print $5}'`
第 1 行生成的报告具有如下数据:
ABC1234:0123456789:ABCDE
ABC4321:9876543210:EDCBA
...
awk 生成的报告与原始报告相同。
报告中没有第五个字段,因为没有空格并且尚未定义不同的字段分隔符。我知道使用 $0 将返回所有字段。指定一个不存在的字段是否会起到同样的作用?
I am trying to understand some scripts that I have inherited and make use of awk. In one of the scripts are these lines:
report=`<make call to Java class that generates a report`
report=`echo $report|awk '{print $5}'`
The report generated in line 1 has data like this:
ABC1234:0123456789:ABCDE
ABC4321:9876543210:EDCBA
...
The awk generated report is the same as the original one.
There is no 5th field in the report since there is no whitespace and a different field separator has not been defined. I know that using $0 will return all fields. Does specifying a field that doesn't exist do the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否:
上面没有打印任何内容。不知道为什么它的行为像您指定的那样。如果您使用
"
而不是'
,那么它将打印出来,因为 $5 会被 shell 扩展,但正如所写的那样,它不应该扩展。No:
The above prints nothing. Don't know why it is behaving like you are specifying. If you were to use
"
instead of'
, then it would print because $5 would be expanded by shell, but as written it should not.您的测试有问题。
在这种情况下,预期的 awk 行为是为每个输入行打印一个空行,这就是我在使用 1TA 或 gawk 运行时看到的结果。
Something is wrong with your test.
The expected awk behavior in this case is to print a blank line for each input line, and that's what I see when I run with either the 1TA or gawk.