English 中文(简体)
Unix脚本中的Awk函数返回错误的列和
原标题:Awk function in Unix script returning wrong Sum of the column
  • 时间:2023-06-27 08:21:43
  •  标签:
  • awk

我有一个脚本使用awk对一列进行求和。文件有3000万条记录,实际总数为

13367024114246662

但是随着awk的出现

13367024114231658

不确定是什么导致了差异。

使用的命令:

awk -v var=$1 -F”^G” ‘{total += $var} END {printf(“%28d/n”,total)}’ $file | sed ‘s/^[ ]*//‘ | sed ‘s/ [ ]*$//‘

记录如下

2008328721^G226^G16^G^G^G^G20230627^G0

尝试了不同的awk方法,但仍然存在差异。

问题回答

awk数字类型是浮点,这是不精确的。这个数字足够大,足以显示该类型的不精确性。

例如,在我的机器上:

janm@Laptop: ~ $ awk  END { print 13367024114231658 + 1}  < /dev/null
13367024114231660
janm@Laptop: ~ $

每次加法的累积误差显示在结果中。

如果使用GNU awk并且它是与MPFR和MP库一起编译的,则可以使用-M/-bignum标志。

在我的系统上:

$ gawk --version
GNU Awk 5.2.1, API 3.2, PMA Avon 8-g1, (GNU MPFR 4.2.0, GNU MP 6.2.1)
^^^^^^^                                     ^^^^            ^^

$ man gawk
...
       -M, --bignum
              Force arbitrary precision arithmetic on numbers. This option has
              no effect if gawk is not compiled to use the GNU  MPFR  and  GMP
              libraries.  (In such a case, gawk issues a warning.)
...

使用janm的示例:

$ awk -M  BEGIN { print 13367024114231658 + 1} 
13367024114231659

$ awk --bignum  BEGIN { print 13367024114231658 + 1} 
13367024114231659

谢谢,不幸的是-M和-bingnum不起作用,关于其他处理方法的任何建议。谢谢





相关问题
awk save command ouput to variable

I need to execute a command per line of some file. For example: file1.txt 100 4 file2.txt 19 8 So my awk script need to execute something like command $1 $2 $3 and save the output of command $1 $2 ...

awk and bash script

I have a tgz file which contains a version file, version.txt. This file has only one line "version=1.0.0". This tgz file is present in two different directories and has the same name. My requirement ...

awk - how to specify field separator as binary value 0x1

Is it possible to specify the separator field FS in binary for awk? I have data file with ascii data fields but separated by binary delimiter 0x1. If it was character 1 it would look like this: ...

Awk - print next record following matched record

I m trying to get a next field after matching field using awk. Is there an option to do that or do I need to scan the record into array then check each field in array and print the one after that? ...

Using Unix Tools to Extract String Values

I wrote a small Perl script to extract all the values from a JSON formatted string for a given key name (shown below). So, if I set a command line switch for the Perl script to id, then it would ...

how to use sed, awk, or gawk to print only what is matched?

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract ...

Bulk Insert Code Before </body> Tag in 100 Files

I d like to insert <?php include_once( google_analytics.php ); ?> before the closing body tag of about 100 php files. Unfortunately the person who made the site didn t make a header or ...

热门标签