Answers:
alias sum="paste -sd+ - | bc"
添加到外壳完成中,感谢队友
. . .| x=$(echo <(cat)); echo $((0+${x// /+}+0))
如果您一直都想参加bash:
/usr/bin/sum
bc
在某些系统上不可用!awk
另一方面,(我认为)要符合POSIX要求。
开始
cat files.txt | xargs ls -l | cut -c 23-30 |
awk '{total = total + $1}END{print total}'
cut
呢?这是一个可预见的列数,所以使用... | xargs ls -l | awk '{total = total + $5}{END{print total}'
total+=$1
,而不是total = total + $1
python3 -c"import os; print(sum(os.path.getsize(f) for f in open('files.txt').read().split()))"
或者,如果您只想对数字求和,则输入:
python3 -c"import sys; print(sum(int(x) for x in sys.stdin))"
... | python -c'import sys; print(sum(int(x) for x in sys.stdin))'
当python 2在今年年底消失时。
当您拥有stat时,整个ls -l以及随后的cut都是相当复杂的。它也容易受到ls -l确切格式的影响(直到我更改了cut的列号后才起作用)
<files.txt xargs stat -c %s | paste -sd+ - | bc
<infile command
是一样的(并且比更好)command <infile
。
我会改用“ du”。
$ cat files.txt | xargs du -c | tail -1
4480 total
如果您只想要数字:
cat files.txt | xargs du -c | tail -1 | awk '{print $1}'
files.txt
大的时候这是行不通的。如果传递给的参数数量xargs
达到某个阈值,则会在多次调用时将其分解du
。最后显示的总数是最后一次调用的总数du
,而不是整个列表的总数。
在ksh中:
echo " 0 $(ls -l $(<files.txt) | awk '{print $5}' | tr '\n' '+') 0" | bc
cut
,但是您会忽略awk的数学能力……
#
# @(#) addup.sh 1.0 90/07/19
#
# Copyright (C) <heh> SjB, 1990
# Adds up a column (default=last) of numbers in a file.
# 95/05/16 updated to allow (999) negative style numbers.
case $1 in
-[0-9])
COLUMN=`echo $1 | tr -d -`
shift
;;
*)
COLUMN="NF"
;;
esac
echo "Adding up column .. $COLUMN .. of file(s) .. $*"
nawk ' OFMT="%.2f" # 1 "%12.2f"
{ x = '$COLUMN' # 2
neg = index($x, "$") # 3
if (neg > 0) X = gsub("\\$", "", $x)
neg = index($x, ",") # 4
if (neg > 1) X = gsub(",", "", $x)
neg = index($x, "(") # 8 neg (123 & change
if (neg > 0) X = gsub("\\(", "", $x)
if (neg > 0) $x = (-1 * $x) # it to "-123.00"
neg = index($x, "-") # 5
if (neg > 1) $x = (-1 * $x) # 6
t += $x # 7
print "x is <<<", $x+0, ">>> running balance:", t
} ' $*
# 1. set numeric format to eliminate rounding errors
# 1.1 had to reset numeric format from 12.2f to .2f 95/05/16
# when a computed number is assigned to a variable ( $x = (-1 * $x) )
# it causes $x to use the OFMT so -1.23 = "________-1.23" vs "-1.23"
# and that causes my #5 (negative check) to not work correctly because
# the index returns a number >1 and to the neg neg than becomes a positive
# this only occurs if the number happened to b a "(" neg number
# 2. find the field we want to add up (comes from the shell or defaults
# to the last field "NF") in the file
# 3. check for a dollar sign ($) in the number - if there get rid of it
# so we may add it correctly - $12 $1$2 $1$2$ $$1$$2$$ all = 12
# 4. check for a comma (,) in the number - if there get rid of it so we
# may add it correctly - 1,2 12, 1,,2 1,,2,, all = 12 (,12=0)
# 5. check for negative numbers
# 6. if x is a negative number in the form 999- "make" it a recognized
# number like -999 - if x is a negative number like -999 already
# the test fails (y is not >1) and this "true" negative is not made
# positive
# 7. accumulate the total
# 8. if x is a negative number in the form (999) "make it a recognized
# number like -999
# * Note that a (-9) (neg neg number) returns a postive
# * Mite not work rite with all forms of all numbers using $-,+. etc. *
cat files.txt | awk '{ total += $1} END {print total}'
您可以使用awk进行相同的操作,甚至跳过非整数
$ cat files.txt
1
2.3
3.4
ew
1
$ cat files.txt | awk '{ total += $1} END {print total}'
7.7
或者您可以使用ls命令并计算可读的输出
$ ls -l | awk '{ sum += $5} END {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; }'
15.69 Mb
$ ls -l *.txt | awk '{ sum += $5} END {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; }'
2.10 Mb
awk '{ total += $1} END {print total}' files.txt
更快
sizes=( $(cat files.txt | xargs ls -l | cut -c 23-30) )
total=$(( $(IFS="+"; echo "${sizes[*]}") ))
或者您可以在阅读尺码时将它们加起来
declare -i total=0
while read x; total+=x; done < <( cat files.txt | xargs ls -l | cut -c 23-30 )
如果您不在乎咬的大小和块,可以
declare -i total=0
while read s junk; total+=s; done < <( cat files.txt | xargs ls -s )
如果您有R,则可以使用:
> ... | Rscript -e 'print(sum(scan("stdin")));'
Read 4 items
[1] 2232320
由于我对R感到很满意,所以实际上我对这样的事情有几个别名,因此我可以在bash
不记住该语法的情况下使用它们。例如:
alias Rsum=$'Rscript -e \'print(sum(scan("stdin")));\''
让我做
> ... | Rsum
Read 4 items
[1] 2232320