awk打印出错 – 没有足够的参数来满足格式字符串

huangapple go评论47阅读模式
英文:

awk print getting error - not enough arguments to satisfy format string

问题

在awk打印时,我遇到以下错误:

awk: cmd. line:1: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy format string
            `MiNirzRjlOth%ER48R6K0SGDY!'
                         ^ ran out for this one

使用的代码如下:

ABC=`echo ${ABC1}| awk '{printf $NF}'`

其中ABC1的值是随机字符串。

英文:

I am getting below error for awk print

awk: cmd. line:1: (FILENAME=- FNR=1) fatal: not enough arguments to satisfy format string
            `MiNirzRjlOth%ER48R6K0SGDY!'
                         ^ ran out for this one

code using

ABC=`echo ${ABC1}| awk '{printf $NF}'`

where the value of ABC1 is random string

答案1

得分: 3

永远不要对任何输入数据($1,$NF,$0或任何保存先前读取的输入数据的变量)执行 printf input_data,因为当你的输入数据包含类似 %s 的 printf 格式字符时,它会失败,就像你现在看到的 %E... 在你的输入中出现一样。改为执行 printf "%s", input_data

英文:

Never do printf input_data for any input data ($1, $NF, $0, or any variable that holds previously read input data) as it'll fail when your input data contains printf formatting chars like %s, as you're now seeing with %E... appearing in your input. Do printf "%s", input_data instead.

答案2

得分: 1

printf 在 awk 中是使用格式模板打印内容。文档在这里。如果你只想打印变量本身,就不需要使用它。

你已经指定了 $NF - 它是“变量数量等于变量数量”,实际上是“最后一个变量”。

在你的情况下,你可以使用以下任一种方法,它们会给出相同的结果:

awk '{print $NF}'
MiNirzRjlOth%ER48R6K0SGDY!

或者使用格式化。对于字符串,代码是 %s

awk '{printf "%s", $NF}'
MiNirzRjlOth%ER48R6K0SGDY!

使用 printf 的优点是你可以填充数据,左右调整等等。

英文:

printf in awk is to print something using a format template. Documentation here. If you just want to print the variable as is, you don't need to use this.

You have specified $NF - which is "variable number equal to count of variables", effectively "last variable".

In your case you can one of the following, which will give the same result:

 `awk '{print $NF}'`  
 MiNirzRjlOth%ER48R6K0SGDY!

or using formatting. For strings code %s is used.

 `awk '{printf "%s", $NF}'`
 MiNirzRjlOth%ER48R6K0SGDY!

Using printf has the advantage that you can pad with data, left/right adjust and much more.

huangapple
  • 本文由 发表于 2023年2月14日 20:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定