bash AWK 语法错误 - 是什么原因造成的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8474276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AWK syntax error - what's causing it?
提问by user710818
I have simple bash script:
我有简单的 bash 脚本:
#!/bin/sh
column=${1:-1}
awk ' {colawk='$column'+2; print $colawk}'
awk '(x=4; print $x)'
But I have received error:
但我收到了错误:
awk: (x=4; print $x)
awk: ^ syntax error
awk: cmd. line:1: (x=4; print $x)
awk: cmd. line:1: ^ unexpected newline or end of string
Why? Code in the previous line works.
为什么?上一行中的代码有效。
采纳答案by paxdiablo
Your problem is with using parentheses instead of braces. Try:
您的问题是使用括号而不是大括号。尝试:
awk '{x=4; print $x}'
instead, as in the following transcript:
相反,如以下成绩单所示:
pax$ echo a b c d e | awk '(x=4; print $x)'
awk: cmd. line:1: (x=4; print $x)
awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: (x=4; print $x)
awk: cmd. line:2: ^ unexpected newline or end of string
pax$ echo a b c d e | awk '{x=4; print $x}'
d
回答by kev
An AWK program is a series of pattern action pairs, written as:
一个 AWK 程序是一系列模式动作对,写成:
condition { action }
where condition
is typically an expression and action
is a series of commands.
wherecondition
通常是一个表达式,action
是一系列命令。
print
is not expression but a statement, so it's a syntax erroras expected.
print
不是表达式而是语句,因此它是预期的语法错误。