Add up a column of numbers at the Unix shell

后端 未结 20 2245
Happy的楠姐
Happy的楠姐 2021-01-29 18:10

Given a list of files in files.txt, I can get a list of their sizes like this:

cat files.txt | xargs ls -l | cut -c 23-30

which pr

20条回答
  •  囚心锁ツ
    2021-01-29 18:19

    If you have R, you can use:

    > ... | Rscript -e 'print(sum(scan("stdin")));'
    Read 4 items
    [1] 2232320
    

    Since I'm comfortable with R, I actually have several aliases for things like this so I can use them in bash without having to remember this syntax. For instance:

    alias Rsum=$'Rscript -e \'print(sum(scan("stdin")));\''
    

    which let's me do

    > ... | Rsum
    Read 4 items
    [1] 2232320
    

    Inspiration: Is there a way to get the min, max, median, and average of a list of numbers in a single command?

提交回复
热议问题