在 bash 中处理超出范围的整数
我已将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该首先打印
$USED
,awk
中的变量是USED
:您的情况最有可能发生的是您使用的
awk
的限制是每条记录大约 32000 个字段。因为您的字段
4
、5
和6
分别是25172
、664
和 < code>8520(来自您的评论之一),您的USED
值将变为4042928-25172-664-8520
或4008572
。如果您尝试打印
USED
,这就是您会得到的结果,但是,因为您尝试打印$USED
,它认为您需要$4008572
(字段号 4008572),仅超出 32000 范围一点点。有趣的是,如果你有更多的可用内存,你不会收到错误,但你仍然会得到一个错误的值:-)
顺便说一句,
gawk
没有这个限制,它简单地打印一个空字段(例如,参见第 11.9 节此处)。You should not be printing
$USED
for a start, the variable inawk
isUSED
: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
and6
are respectively25172
,664
and8520
(from one of your comments), yourUSED
value becomes4042928-25172-664-8520
or4008572
.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).你只需使用一个 awk 命令即可完成
you can just do it with one awk command