Linux 在 Unix shell 中添加一列数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/926069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add up a column of numbers at the Unix shell
提问by RichieHindle
Given a list of files in files.txt
, I can get a list of their sizes like this:
给定 中的文件列表files.txt
,我可以获得它们的大小列表,如下所示:
cat files.txt | xargs ls -l | cut -c 23-30
which produces something like this:
它产生这样的东西:
151552
319488
1536000
225280
How can I get the totalof all those numbers?
我怎样才能得到所有这些数字的总和?
采纳答案by Todd Owen
... | paste -sd+ - | bc
is the shortest one I've found (from the UNIX Command Lineblog).
是我找到的最短的一个(来自UNIX 命令行博客)。
Edit:added the -
argument for portability, thanks @Dogbert and @Owen.
编辑:添加了-
可移植性的参数,感谢@Dogbert 和@Owen。
回答by MichaelJones
I would use "du" instead.
我会用“du”代替。
$ cat files.txt | xargs du -c | tail -1
4480 total
If you just want the number:
如果您只想要数字:
cat files.txt | xargs du -c | tail -1 | awk '{print }'
回答by Greg Reynolds
Here goes
开始
cat files.txt | xargs ls -l | cut -c 23-30 |
awk '{total = total + }END{print total}'
回答by 0x6adb015
Pipe to gawk:
管道到呆呆:
cat files.txt | xargs ls -l | cut -c 23-30 | gawk 'BEGIN { sum = 0 } // { sum = sum + #!/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
} END { print sum }'
回答by Andre Miller
You can use the following script if you just want to use shell scripting without awk or other interpreters:
如果您只想在没有 awk 或其他解释器的情况下使用 shell 脚本,您可以使用以下脚本:
cat files.txt | xargs ls -l | cut -c 23-30 | sed -e :a -e '$!N;s/\n/+/;ta' | bc
回答by Jason Punyon
Here's mine
这是我的
echo " 0 $(ls -l $(<files.txt) | awk '{print }' | tr '\n' '+') 0" | bc
回答by Sanjaya R
In ksh:
在 ksh 中:
$ cat files.txt | xargs ls -l | awk '{total += } END {print "Total:", total, "bytes"}'
回答by Barun
Instead of using cutto get the file size from output of ls -l, you can use directly:
您可以直接使用,而不是使用cut从ls -l 的输出中获取文件大小:
#
# @(#) 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 in
-[0-9])
COLUMN=`echo | 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 - $ $$$$ 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. *
Awk interprets "$5" as the fifth column. This is the column from ls -lthat gives you the file size.
Awk 将“$5”解释为第五列。这是来自ls -l的列,它为您提供文件大小。
回答by steven bensky
perl -nle 'chomp; $x+=(stat($_))[7]; END{print $x}' files.txt
回答by ALL
cat will not work if there are spaces in filenames. here is a perl one-liner instead.
如果文件名中有空格, cat 将不起作用。这是一个 perl one-liner 代替。
perl -lne '$t+=-s;END{print $t}' files.txt