在Unix shell中添加一列数字
给出files.txt
中的文件列表,我可以得到这样的大小列表:
cat files.txt | xargs ls -l | cut -c 23-30
这产生了这样的事情:
151552 319488 1536000 225280
我怎样才能得到所有这些数字的总和 ?
... | paste -sd+ - | bc
是我find的最短的一个(从UNIX命令行博客)。
编辑:添加了可移植的参数,感谢@Dogbert和@Owen。
开始
cat files.txt | xargs ls -l | cut -c 23-30 | awk '{total = total + $1}END{print total}'
如果文件名中有空格,猫将无法工作。 这里是一个perl单行代替。
perl -nle 'chomp; $x+=(stat($_))[7]; END{print $x}' files.txt
而不是使用cut从ls -l的输出中获取文件大小,可以直接使用:
$ cat files.txt | xargs ls -l | awk '{total += $5} END {print "Total:", total, "bytes"}'
Awk将“5美元”解释为第五栏。 这是ls -l给出文件大小的列。
TMTWWTDI :Perl有一个文件大小运算符(-s)
perl -lne '$t+=-s;END{print $t}' files.txt
cat files.txt | xargs ls -l | cut -c 23-30 | python -c'import sys; print sum(int(x) for x in sys.stdin)'
在ksh:
echo " 0 $(ls -l $(<files.txt) | awk '{print $5}' | tr '\n' '+') 0" | bc
我会用“du”代替。
$ cat files.txt | xargs du -c | tail -1 4480 total
如果你只是想要的号码:
cat files.txt | xargs du -c | tail -1 | awk '{print $1}'
如果您只是想在没有awk或其他解释器的情况下使用shell脚本,则可以使用以下脚本:
#!/bin/bash total=0 for number in `cat files.txt | xargs ls -l | cut -c 23-30`; do let total=$total+$number done echo $total
当你有统计数据时,整个ls -l然后被削减是相当复杂的。 它也容易受到ls -l的确切格式的影响(直到我改变了列号才能切换 )
另外,修正了猫的无用的用法 。
<files.txt xargs stat -c %s | paste -sd+ - | bc
如果你没有安装bc,请尝试
echo $(( $(... | paste -sd+ -) ))
代替
... | paste -sd+ - | bc
$( )
< – 返回执行命令的值
$(( 1+2 ))
< – 返回评估结果
echo
< – 回显到屏幕
pipe道到gawk:
cat files.txt | xargs ls -l | cut -c 23-30 | gawk 'BEGIN { sum = 0 } // { sum = sum + $0 } END { print sum }'
这是我的
cat files.txt | xargs ls -l | cut -c 23-30 | sed -e :a -e '$!N;s/\n/+/;ta' | 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 ba "(" 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. *
我喜欢用….
echo " 1 2 3 " | sed -e 's,$, + p,g' | dc
他们会显示每一行的总和…
适用于这种情况:
ls -ld $(< file.txt) | awk '{print $5}' | sed -e 's,$, + p,g' | dc
总计是最后一个值…
在我看来,最简单的解决scheme是“expr”unix命令:
s=0; for i in `cat files.txt | xargs ls -l | cut -c 23-30` do s=`expr $s + $i` done echo $s
纯粹的打击
total=0; for i in $(cat files.txt | xargs ls -l | cut -c 23-30); do total=$(( $total + $i )); done; echo $total