我写了一个非常简单的 awk 脚本,它反转文件的每个单词——:
awk '
{
for (i=1;i<=NF;i++)
{ x=""
for(j=length($i);j>0;j--)
x=x substr($0,j,1);
print x
}}' file1
Run Code Online (Sandbox Code Playgroud)
file1的内容如下-:
hello
Run Code Online (Sandbox Code Playgroud)
现在让我感到困惑的是,当我更换 - 时:
print x
Run Code Online (Sandbox Code Playgroud)
和
print $x
Run Code Online (Sandbox Code Playgroud)
我得到了记录的输出,即。我打招呼,而不是olleh的反向输出。
为什么使用 $x 给我 $0 的内容?变量如何在 awk 中工作?它真的很混乱。当我想使用 $x 而只使用 x 时,是否有固定规则?