Linux:使用AWK计算2个值之间的比率

时间:2014-02-16 21:29:00

标签: linux awk syntax-error

我有一个看起来像这样的文本文件:

abcd nasj uasdy 3452 2314134
abcd efgh sdfdsf 234553 9323454
abcd asdf asdfasdf 34545 5234523
abcd sdfa sadfad 435232434 3452435

现在我正在尝试使用awk计算第5列和第4列之间的比率,我尝试了这个:

BEGIN { printf "%10s %10s" "Column1:", "Ratio:%s" } 
        { printf "%10s %10f"  $1, ($6)/($5) }

但我不断收到错误说:

awk: awkRatio.awk:1: fatal: not enough arguments to satisfy format string
    `%10s %10sColumn1:'
            ^ ran out for this one

2 个答案:

答案 0 :(得分:3)

看起来你错过了printf "..."之后的逗号 尝试:

BEGIN { printf "%10s %10s", "Column1:", "Ratio:" } 
    { printf "%10s %10f",  $1, ($5)/($4) }

答案 1 :(得分:1)

有人这样吗?

awk 'BEGIN { printf "%10s %10s","Column1:","Ratio:\n" } file
        { printf "%10s %10f\n" ,$1, ($5/$4) }'
  Column1:    Ratio:
      abcd 670.374855
      abcd  39.749882
      abcd 151.527660
      abcd   0.007932