如何在 awk 中比较浮点变量?

如何在 awk 中比较浮点变量?
awk -F, 'NR==FNR{c[$29]=$29;next} NF{print $29 ((c[$29]==$29)?" ":",mismatch")}' $file1 $file2

file1 中的第 29 个字段(即 $29)的值为 832.9,file2 中的值为 832.9000。如果我比较它们,它们不相等。我认为它被视为字符串而不是数字。

我该如何继续?

答案1

awk比较浮点数:

我刚刚写了一个新脚本:

paste \
    <(awk -F, '{print $29}' file1 ) \
    <(awk -F, '{print $29}' file2 ) \
    | awk '{print $1; print $2; print ($1==$2)?"match" :"mismatch"}'

如果你不喜欢休息,那么printf使用print.


例子

输入文件

cat file1

12,2,12,12,12,12,3,2,53,6,5474,346,567,6578,89,7689,7,987,69869,1,4,5,4,3,4,2,6,21,832.9,9,2
12,2,12,12,12,12,3,2,53,6,5474,346,567,6578,89,7689,7,987,69869,1,4,5,4,3,4,2,6,21,12.329,9,2

cat file2

12,2,12,12,12,12,3,2,53,6,5474,346,567,6578,89,7689,7,987,69869,1,4,5,4,3,4,2,6,21,832.9000,9,2
12,2,12,12,12,12,3,2,53,6,5474,346,567,6578,89,7689,7,987,69869,1,4,5,4,3,4,2,6,21,832.9000,9,2

输出

% paste <(awk -F, '{print $29}' file1 ) <(awk -F, '{print $29}' file2 ) | awk '{print $1; print $2; print ($1==$2)?"match" :"mismatch"}'

832.9
832.9000
match
12.329
832.9000
mismatch

相关内容