在 bash 中处理超出范围的整数

发布于 2024-09-16 13:24:48 字数 423 浏览 5 评论 0原文

我已将 vmstat 数据收集到一个文件中。它提供了有关 free、buffer 和 cache 的详细信息。 由于我对查找内存使用情况感兴趣,因此我应该对 vmstat 输出的每一行进行以下计算 -- USED=TOTAL - (FREE+BUFFER+CACHE) 其中 TOTAL 是总 RAM 内存,USED 是瞬时内存值。 总内存 = 4042928 (4 GB)

我的代码在这里

grep -v procs $1 | grep -v free | awk '{USED=4042928-$4-$5-$6;print $USED}' > test.dat
awk: program limit exceeded: maximum number of fields size=32767
        FILENAME="-" FNR=1 NR=1

I have collected vmstat data in a file . It gives details about free, buffer and cache .
SInce i'm interested in finding the memory usage , I should do the following computation for each line of vmstat output -- USED=TOTAL - (FREE+BUFFER+CACHE) where TOTAL is the total RAM memory and USED is the instantaneous memory value.
TOTAL memory = 4042928 (4 GB)

My code is here

grep -v procs $1 | grep -v free | awk '{USED=4042928-$4-$5-$6;print $USED}' > test.dat
awk: program limit exceeded: maximum number of fields size=32767
        FILENAME="-" FNR=1 NR=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哥,最终变帅啦 2024-09-23 13:24:48

您不应该首先打印 $USEDawk 中的变量是 USED

pax> vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0      0 1402804 258392 2159316    0    0    54    79  197  479  1  2 93  3

pax> vmstat | egrep -v 'procs|free' | awk '{USED=4042928-$4-$5-$6;print USED}'
222780

您的情况最有可能发生的是您使用的 awk 的限制是每条记录大约 32000 个字段。

因为您的字段 456 分别是 25172664 和 < code>8520(来自您的评论之一),您的 USED 值将变为 4042928-25172-664-85204008572

如果您尝试打印 USED,这就是您会得到的结果,但是,因为您尝试打印 $USED,它认为您需要 $4008572(字段号 4008572),仅超出 32000 范围一点点

有趣的是,如果你有更多的可用内存,你不会收到错误,但你仍然会得到一个错误的值:-)

顺便说一句,gawk没有这个限制,它简单地打印一个空字段(例如,参见第 11.9 节此处)。

You should not be printing $USED for a start, the variable in awk is USED:

pax> vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0      0 1402804 258392 2159316    0    0    54    79  197  479  1  2 93  3

pax> vmstat | egrep -v 'procs|free' | awk '{USED=4042928-$4-$5-$6;print USED}'
222780

What is most likely to be happening in your case is that you're using an awk with that limitation of about 32000 fields per record.

Because your fields 4, 5 and 6 are respectively 25172, 664 and 8520 (from one of your comments), your USED value becomes 4042928-25172-664-8520 or 4008572.

If you tried to print USED, that's what you'd get but, because you're trying to print $USED, it thinks you want $4008572 (field number 4008572) which is just a little bit beyond the 32000 range.

Interestingly, if you have a lot more free memory, you wouldn't get the error but you'd still get an erroneous value :-)

By the way, gawk doesn't have this limitation, it simply prints an empty field (see, for example, section 11.9 here).

吃兔兔 2024-09-23 13:24:48

你只需使用一个 awk 命令即可完成

vmstat | awk 'NR>2{print 4042928-$4-$5-$6 }' file

you can just do it with one awk command

vmstat | awk 'NR>2{print 4042928-$4-$5-$6 }' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文