1. [root@1E5BB3B8E8B51497 ~]# echo -e '1\n2\n3'
  2. 1
  3. 2
  4. 3

以下两条命令都能得到结果 6

  1. echo -e '1\n2\n3' | awk '{for(n=1;n<=NF;n++)t[n]+=$n}END{for(n=1;n<=NF;n++)printf t[n]}'
  1. echo -e '1\n2\n3' | awk '{sum+=$1}END{print sum}'

image.png

试验一下

  1. echo -e '1\n2\n3' > num.txt
  2. cat num.txt
  3. cat num.txt | awk '{sum += $1} END {print sum}'
  4. rm -rf num.txt
  1. df -B 1g | grep 'disk' | awk '{print $2}' | awk '{sum+=$1}END{print sum}'