给定files.txt中的文件列表,我可以得到它们的大小列表,如下所示:
cat files.txt | xargs ls -l | cut -c 23-30
这会产生这样的结果:
151552
319488
1536000
225280
我怎样才能得到所有这些数字的总数呢?
给定files.txt中的文件列表,我可以得到它们的大小列表,如下所示:
cat files.txt | xargs ls -l | cut -c 23-30
这会产生这样的结果:
151552
319488
1536000
225280
我怎样才能得到所有这些数字的总数呢?
当前回答
... |xargs|tr \ +|bc
... |paste -sd+ -|bc
第一个命令只是多了一个符号(注意,它必须在反斜杠后面有两个空格!),但它处理列中有空行的情况,而第二个命令导致带有额外加号的无效表达式。
例如:
echo "2
3
5
" | paste -sd+ -
结果
2+3+5++
哪个BC不能处理,而
echo "2
3
5
" | xargs | tr \ +
给出一个有效表达式
2+3+5
哪些可以被导入BC以获得最终结果
其他回答
#
# @(#) 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. *
如果你有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
灵感:有没有一种方法可以在一个命令中获得一组数字的最小值、最大值、中值和平均值?
当管道的开始可以产生0行时,最流行的答案是不正确的,因为它最终输出的是0而不是什么。你可以通过总是加0来得到正确的行为:
... | (cat && echo 0) |粘贴-sd+ - | BC
... | paste -sd+ - | bc
是我找到的最短的一个(来自UNIX命令行博客)。
编辑:增加了-参数的可移植性,谢谢@Dogbert和@Owen。
如果您没有安装BC,请尝试
echo $(( $(... | paste -sd+ -) ))
而不是
... | paste -sd+ - | bc
$() <——返回执行命令的值
$((1+2)) <——返回求值结果
Echo <——将它回显到屏幕