在 awk 中,我们$
用来提取由IFS
awk `/LIG/{print $1, $2}' topol.top
Run Code Online (Sandbox Code Playgroud)
在 shell 中,我们$
用于提取我们为变量存储的值。例如。for
环形。
for i in *; do
mv -- $i $i.pdb; done
Run Code Online (Sandbox Code Playgroud)
(根据答案更正)
是$
在这两个背景下本质上是相同的或不同的使用情况如何?
如果不同,为什么会不同?如何将awk和shell$
混在一起使用?
$
在 shell 和$
awk 中只是看起来它们是相关的,因为有时$
在 shell之后可以有一个数字,有时在后面有一个变量,在 awk 中也是如此,但它们没有任何关系,只是 2 使用的相同字符在 2 个完全不同的上下文中完全不同的工具,shell 中的内容$<number>
和$<variable>
含义与它们在 awk 中的含义完全不同。
在 shell 中,您可以通过添加变量来取消引用变量,$
并且位置参数等在这方面被视为与变量相同,因此您可以拥有:
$0 = the path to the current command
$1 = the first argument passed to the command
$2 = the second argument passed to the command
$# = the number of arguments passed to the command
$? = the exit status of the last command run
$var = the value of the shell variable var
etc.
Run Code Online (Sandbox Code Playgroud)
在 awk 中$
是当前记录拆分成的字段数组(概念上)的名称,因此您只能$
在这些表达式中使用:
$0 = the current record being processed
$1 = the first field of the current record
$2 = the second field of the current record
Run Code Online (Sandbox Code Playgroud)
要取消引用 awk 变量var
,您只需使用它的 name var
,就像在 C 中一样,实际上 awk 语法与 C 的相似度远高于与 shell 的相似度。
如果您曾经$var
在 awk 中看到使用过,那么它不是$
解引用var
,而是var
单独使用名称进行解引用var
,如果var
有一个数值,例如 say, 5
,则$var
表示与$5
当前记录的第 5 个字段相同,如果var
是没有数值则$var
意味着与$0
当前记录相同:
var=0 => $var = $0 = the current record being processed
var=1 => $var = $1 = the first field of the current record
var=2 => $var = $2 = the second field of the current record
var="" => $var = $0 = the current record being processed
var="foo" => $var = $0 = the current record being processed
Run Code Online (Sandbox Code Playgroud)
awk 和 shell 是 2 个完全不同的工具,它们有自己的语法、语义和变量范围等。所以就这样对待它们,不要假设你在 awk 脚本中看到的任何东西与你在 a语法、语义或范围内的 shell 脚本,反之亦然。