Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何用字节、KiB、MiB、GiB从ls(如输出日志)求和文件大小_Bash_Awk_Ls - Fatal编程技术网

Bash 如何用字节、KiB、MiB、GiB从ls(如输出日志)求和文件大小

Bash 如何用字节、KiB、MiB、GiB从ls(如输出日志)求和文件大小,bash,awk,ls,Bash,Awk,Ls,我有一个预先计算的ls类输出(它不是来自实际的ls命令),我无法修改或重新计算它。如下所示: 2016-10-14 14:52:09 0 Bytes folder/ 2020-04-18 05:19:04 201 Bytes folder/file1.txt 2019-10-16 00:32:44 201 Bytes folder/file2.txt 2019-08-26 06:29:46 201 Bytes folder/file3.txt 2020-07-08 16:13:56

我有一个预先计算的
ls
类输出(它不是来自实际的
ls
命令),我无法修改或重新计算它。如下所示:

2016-10-14 14:52:09    0 Bytes folder/
2020-04-18 05:19:04  201 Bytes folder/file1.txt
2019-10-16 00:32:44  201 Bytes folder/file2.txt
2019-08-26 06:29:46  201 Bytes folder/file3.txt
2020-07-08 16:13:56  411 Bytes folder/file4.txt
2020-04-18 03:03:34  201 Bytes folder/file5.txt
2019-10-16 08:27:11    1.1 KiB folder/file6.txt
2019-10-16 10:13:52  201 Bytes folder/file7.txt
2019-10-16 08:44:35  920 Bytes folder/file8.txt
2019-02-17 14:43:10  590 Bytes folder/file9.txt
日志可能至少有
GiB
MiB
KiB
字节。可能值中包括零值,或每个前缀都带有逗号的值:

0   Bytes
3.9 KiB
201 Bytes
2.0 KiB
2.7 MiB
1.3 GiB
类似的方法如下所示

awk 'BEGIN{ pref[1]="K";  pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x  > 1024 ) { x = (x + 1023)/1024; y++; }  printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while(  total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total:  %g%s\n",int(total*10)/10,pref[y]); }'
但在我的情况下不能正常工作:

$ head -n 10 files_sizes.log | awk '{print $3,$4}' | sort | awk 'BEGIN{ pref[1]="K";  pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x  > 1024 ) { x = (x + 1023)/1024; y++; }  printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while(  total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total:  %g%s\n",int(total*10)/10,pref[y]); }'


0K  Bytes
1.1K    KiB
201K    Bytes
201K    Bytes
201K    Bytes
201K    Bytes
201K    Bytes
411K    Bytes
590K    Bytes
920K    Bytes
Total:  3.8M
此输出错误地计算了大小。我的desidered输出是输入日志文件的正确总和:

0 Bytes
201 Bytes
201 Bytes
201 Bytes
411 Bytes
201 Bytes
1.1 KiB
201 Bytes
920 Bytes
590 Bytes
Total:  3.95742 KiB
注意

字节之和的正确值为
201*5+590+920=2926,因此将
KiB
相加的总数为 2.857422+1.1=395742 KiB=4052.400字节

[更新]

我更新了与和解决方案的结果比较,得出了几乎相同的值:

$ head -n 10 files_sizes.log | tr -s ' ' | cut -d' ' -f3,4 | sed 's/ //; s/Bytes//; s/B//' | gnumfmt --from=auto | awk '{s+=$1}END{print s " Bytes"}'
117538 Bytes
$ head -n 1000 files_sizes.log | tr -s ' ' | cut -d' ' -f3,4 | sed 's/ //; s/Bytes//; s/B//' | gnumfmt --from=auto | awk '{s+=$1}END{print s " Bytes"}'
1225857 Bytes
$ head -n 10000 files_sizes.log | tr -s ' ' | cut -d' ' -f3,4 | sed 's/ //; s/Bytes//; s/B//' | gnumfmt --from=auto | awk '{s+=$1}END{print s " Bytes"}'
12087518 Bytes
$ head -n 1000000 files_sizes.log | tr -s ' ' | cut -d' ' -f3,4 | sed 's/ //; s/Bytes//; s/B//' | gnumfmt --from=auto | awk '{s+=$1}END{print s " Bytes"}'
77238840381 Bytes
$ head -n 100000000 files_sizes.log | tr -s ' ' | cut -d' ' -f3,4 | sed 's/ //; s/Bytes//; s/B//' | gnumfmt --from=auto | awk '{s+=$1}END{print s " Bytes"}'
2306569381835 Bytes

在哪里

2.097807 TiB=2.3065631893 TB=2306569381835字节

计算上,我比较了所有三种解决方案的速度:

$ time head -n 100000000 files_sizes.log | ./count_files.sh
2.097807 TiB

real    2m7.956s
user    2m10.023s
sys 0m1.696s

$ time head -n 100000000 files_sizes.log | tr -s ' ' | cut -d' ' -f3,4 | sed 's/ //; s/Bytes//; s/B//' | gnumfmt --from=auto | awk '{s+=$1}END{print s " Bytes"}'
2306569381835 Bytes

real    4m12.896s
user    5m45.750s
sys 0m4.026s

$ time (head -n 100000000 files_sizes.log | tr -s ' ' | cut -d' ' -f3,4 | sed 's/Bytes//; s/KiB/* 1024/; s/MiB/* 1024 * 1024/;s/GiB/* 1024 * 1024 * 1024/; s/$/ + /; $s/+ //' | tr -d '\n' ; echo) | bc
2306563692898.8

real    4m31.249s
user    6m40.072s
sys 0m4.252s

让我为您提供一种更好的使用
ls
的方法:不要将其用作命令,而是用作
find
开关:

find . -maxdepth 1 -ls
这将以统一的单位返回文件大小,如
find
的手册页中所述,这使计算更加容易


祝您好运

输入如下所述:

2016-10-14 14:52:09    0 Bytes folder/
2020-04-18 05:19:04  201 Bytes folder/file1.txt
2019-10-16 00:32:44  201 Bytes folder/file2.txt
2019-08-26 06:29:46  201 Bytes folder/file3.txt
2020-07-08 16:13:56  411 Bytes folder/file4.txt
2020-04-18 03:03:34  201 Bytes folder/file5.txt
2019-10-16 08:27:11    1.1 KiB folder/file6.txt
2019-10-16 10:13:52  201 Bytes folder/file7.txt
2019-10-16 08:44:35  920 Bytes folder/file8.txt
2019-02-17 14:43:10  590 Bytes folder/file9.txt
您可以使用您希望能够解码的单位表:

BEGIN {
    unit["Bytes"] = 1;

    unit["kB"] = 10**3;
    unit["MB"] = 10**6;
    unit["GB"] = 10**9;
    unit["TB"] = 10**12;
    unit["PB"] = 10**15;
    unit["EB"] = 10**18;
    unit["ZB"] = 10**21;
    unit["YB"] = 10**24;

    unit["KB"] = 1024;
    unit["KiB"] = 1024**1;
    unit["MiB"] = 1024**2;
    unit["GiB"] = 1024**3;
    unit["TiB"] = 1024**4;
    unit["PiB"] = 1024**5;
    unit["EiB"] = 1024**6;
    unit["ZiB"] = 1024**7;
    unit["YiB"] = 1024**8;
}
然后在主循环中进行总结:

{
    if($4 in unit) total += $3 * unit[$4];
    else printf("ERROR: Can't decode unit at line %d: %s\n", NR, $0);
}
并在末尾打印结果:

END {
    binaryunits[0] = "Bytes";
    binaryunits[1] = "KiB";
    binaryunits[2] = "MiB";
    binaryunits[3] = "GiB";
    binaryunits[4] = "TiB";
    binaryunits[5] = "PiB";
    binaryunits[6] = "EiB";
    binaryunits[7] = "ZiB";
    binaryunits[8] = "YiB";
    for(i = 8;; --i) {
         if(total >= 1024**i || i == 0) {
            printf("%.3f %s\n", total/(1024**i), binaryunits[i]);
            break;
        }
    }
}
输出:

3.957 KiB
请注意,您可以在awk脚本的开头添加一个she-bang,以使其能够独立运行,从而不必将其放入脚本中:


您可以在将输入发送到
bc
之前对其进行分析:

echo "0   Bytes
3.9 KiB
201 Bytes
2.0 KiB
2.7 MiB
1.3 GiB" |
   sed 's/Bytes//; s/KiB/* 1024/; s/MiB/* 1024 * 1024/; 
        s/GiB/* 1024 * 1024 * 1024/; s/$/ + /'  |
   tr -d '\n' | 
   sed 's/+ $/\n/' |
   bc
当您的
sed
不支持
\n
时,您可以尝试用真正的换行符替换“\n”,如

sed 's/+ $/
/'
或者在解析后添加一个
echo
(并将最后一个
sed
的一部分移动到第一个命令中,以删除最后一个
+


使用
numfmt
转换这些数字

cat <<EOF |
2016-10-14 14:52:09    0 Bytes folder/
2020-04-18 05:19:04  201 Bytes folder/file1.txt
2019-10-16 00:32:44  201 Bytes folder/file2.txt
2019-08-26 06:29:46  201 Bytes folder/file3.txt
2020-07-08 16:13:56  411 Bytes folder/file4.txt
2020-04-18 03:03:34  201 Bytes folder/file5.txt
2019-10-16 08:27:11    1.1 KiB folder/file6.txt
2019-10-16 10:13:52  201 Bytes folder/file7.txt
2019-10-16 08:44:35  920 Bytes folder/file8.txt
2019-02-17 14:43:10  590 Bytes folder/file9.txt
2019-02-17 14:43:10  3.9 KiB  folder/file9.txt
2019-02-17 14:43:10  2.7 MiB folder/file9.txt
2019-02-17 14:43:10  1.3 GiB folder/file9.txt
EOF
# extract 3rd and 4th column
tr -s ' ' | cut -d' ' -f3,4 |
# Remove space, remove "Bytes", remove "B"
sed 's/ //; s/Bytes//; s/B//' |
# convert to numbers
numfmt --from=auto |
# sum
awk '{s+=$1}END{print s}'

cat从@KamilCuk到。根据他的回答,这里有一个替代命令,它使用带有。它需要GNU awk的最新版本(5.0.1版本可以,4.1.4版本不稳定,中间未测试)

注释

  • LC_NUMERIC=C
    (bash/ksh/zsh)用于在使用非英语语言环境的系统上进行移植
  • PROCINFO[conv,“pty”]=1
    允许在每行刷新
    numfmt
    的输出(以避免解除锁定)

请注意:@Cyrus谢谢,我有一个预计算的
ls
类输出(它不是来自实际的
ls
命令),我不能修改或重新计算它。@TEDLYNGOM谢谢,因为原始文件的头不同<代码>2020-09-02 18:03:20 275.0 KiB my_file.txt
因此我去掉了其他列。请将示例输入(无描述、无图像、无链接)和您对该示例输入的所需输出添加到您的问题中(无注释)。您说过您无法重新计算该文件。当你(或其他发现相同问题的人)确实有访问权限时,你可能想使用类似于
du-bc$(find folder-type f-name“*.txt”| tail-1)
。如果我有
head-n10 files_size.log | awk'{print$3,$4}sed's/Bytes/,谢谢;s/KiB/*1024/;s/MiB/*1024*1024/;s/GiB/*1024*1024*1024/;s/$/+/'| tr-d'\n'| sed的/+$/\n/'| bc
I get
(标准中)1:解析错误
它与上面的我的
echo
一起工作吗?再次尝试:删除最后一部分
|bc
,然后查看您看到的字符串。你希望像
0+3.9*1024+201+2.0*1024+2.7*1024*1024+1.3*1024*1024*1024*1024
,奇怪的事情需要解决(“sed”)。好的,没有
bc
我可以看到
0+201+201+411+201+1.1*1024+201+920+590n
。请注意,我在
macOS上
。编辑了我的解决方案。您需要输入周围的
()
,如
(head-n…echo)| bc
。问题中没有提到,但在注释中指出输出不是来自
ls
本身。这是一个预先计算的
ls
类输出(不是来自实际的
ls
命令),OP无法修改或重新计算它谢谢
head-n10 files_size.log |/count_files.sh3.957422 KiB
@loretoparisi在完全接受我的答案之前,我发现KamilCuk的答案有一点令人不安,因为我的答案是
2.097807 TiB
。你能按原样打印
total
来检查我的脚本是否正确吗?它是否也打印
2306569381835
?对于这个总数,我希望我的脚本输出
2.097813 TiB
。可能是这样,KamilCuk的脚本能够解码数据中的某些行,而我的脚本无法解码。嗯……它打印出
2.097807 TiB
。只是为了确定我是这样做的——我明白了
numfmt
直接进行四舍五入,结果额外增加5688992.542480字节。我的只是将浮点数相加,然后进行四舍五入。好的,那么我同意答案的原样。另外,我还补充说,运行更新的代码时不会出现任何错误。还需要指出的是,这个解决方案也是更快的解决方案(~1,5倍于其他解决方案)。
numfmt
非常好,所以我投你一票。我不明白为什么
K=1000
numfmt
k=1000
k=1024
在我的书中。谢谢,因为我在
macOS
上,我不得不用
gnumfmt
替换
numfmt
。我想你需要添加选项
--from=iec
,试试看
echo "0   Bytes
3.9 KiB
201 Bytes
2.0 KiB
2.7 MiB
1.3 GiB" |
   sed 's/Bytes//; s/KiB/* 1024/; s/MiB/* 1024 * 1024/; 
        s/GiB/* 1024 * 1024 * 1024/; s/$/ + /'  |
   tr -d '\n' | 
   sed 's/+ $/\n/' |
   bc
sed 's/+ $/
/'
(echo "0   Bytes
3.9 KiB
201 Bytes
2.0 KiB
2.7 MiB
1.3 GiB" | sed 's/Bytes//; s/KiB/* 1024/; s/MiB/* 1024 * 1024/;
s/GiB/* 1024 * 1024 * 1024/; s/$/ + /; $s/+ //'  | tr -d '\n' ; echo) | bc
cat <<EOF |
2016-10-14 14:52:09    0 Bytes folder/
2020-04-18 05:19:04  201 Bytes folder/file1.txt
2019-10-16 00:32:44  201 Bytes folder/file2.txt
2019-08-26 06:29:46  201 Bytes folder/file3.txt
2020-07-08 16:13:56  411 Bytes folder/file4.txt
2020-04-18 03:03:34  201 Bytes folder/file5.txt
2019-10-16 08:27:11    1.1 KiB folder/file6.txt
2019-10-16 10:13:52  201 Bytes folder/file7.txt
2019-10-16 08:44:35  920 Bytes folder/file8.txt
2019-02-17 14:43:10  590 Bytes folder/file9.txt
2019-02-17 14:43:10  3.9 KiB  folder/file9.txt
2019-02-17 14:43:10  2.7 MiB folder/file9.txt
2019-02-17 14:43:10  1.3 GiB folder/file9.txt
EOF
# extract 3rd and 4th column
tr -s ' ' | cut -d' ' -f3,4 |
# Remove space, remove "Bytes", remove "B"
sed 's/ //; s/Bytes//; s/B//' |
# convert to numbers
numfmt --from=auto |
# sum
awk '{s+=$1}END{print s}'
LC_NUMERIC=C gawk '
    BEGIN {
        conv = "numfmt --from=auto"
        PROCINFO[conv, "pty"] = 1
    }
    {
        sub(/B.*/, "", $4)
        print $3 $4 |& conv
        conv |& getline val
        sum += val
    }
    END { print sum }
' input