{"id":1585,"date":"2014-11-14T14:36:29","date_gmt":"2014-11-14T14:36:29","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1585"},"modified":"2014-11-14T14:36:29","modified_gmt":"2014-11-14T14:36:29","slug":"%e7%94%a8%e4%b8%80%e8%a1%8cshell%e5%91%bd%e4%bb%a4%e5%81%9a%e7%b4%af%e5%8a%a0%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1585.html","title":{"rendered":"\u7528\u4e00\u884cshell\u547d\u4ee4\u505a\u7d2f\u52a0\u64cd\u4f5c"},"content":{"rendered":"<p>\u5728Stackoverflow\u4e0a\u770b\u5230\u7684\u4e00\u5219\u95ee\u7b54\uff0c\u4e4b\u524d\u6709\u6536\u96c6\u8fc7\u4e00\u4e9b\uff0c\u5728\u91cc\u9762\u90fd\u5305\u62ec\u4e86\uff0c\u8fd8\u6269\u5145\u4e86\u4e0d\u5c11\uff0c\u503c\u5f97\u5b66\u4e60\uff0c\u5c31\u6b64\u8bb0\u5f55\u5982\u4e0b\uff08\u5b9e\u4f8b\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true \"># cat num.txt\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n\n# awk '{s+=$1} END {print s}' num.txt\n55\n# paste -s -d+ num.txt | bc\n55\n# cat num.txt | python -c \"import sys; print sum(int(l) for l in sys.stdin)\"\n55\n# cat num.txt | perl -lpe '$c+=$_}{$_=$c'\n55\n# cat num.txt | xargs | sed -e 's\/ \/+\/g' | bc\n55\n# cat num.txt | xargs\n1 2 3 4 5 6 7 8 9 10\n\n# cat num.txt | perl -MList::Util -le 'print List::Util::sum(&lt;&gt;)'\n55\n# sed 's\/^\/.+\/' num.txt | bc | tail -1\n55\n# sed 's\/^\/.+\/' num.txt | bc\n1\n3\n6\n10\n15\n21\n28\n36\n45\n55<\/pre>\n<p>\u6700\u65b9\u4fbf\u7684\u8fd8\u662fawk\u554a\uff0c\u5176\u6b21\u5c31\u662fsed\/xargs\u52a0\u4e0abc\u7684\u7ec4\u5408\u4e86\uff0cPerl\u3001Python\u4ec0\u4e48\u7684\u4e5f\u4e0d\u9519\u3002<\/p>\n<pre class=\"lang:default decode:true \">1.paste\u547d\u4ee4\nUsing existing file:\npaste -sd+ infile | bc\n\nUsing stdin:\n&lt;cmd&gt; | paste -sd+ | bc\n\nEdit: With some paste implementations you need to be more explicit when reading from stdin:\n&lt;cmd&gt; | paste -sd+ - | bc\n\n2.awk\u547d\u4ee4\uff08\u6548\u7387\u9ad8\u4e8e\u4e0a\u9762\u7684paste\u547d\u4ee4\uff09\nI like the chosen answer. However, it tends to be slower than awk since 2 tools are needed to do the job.\n\n$ wc -l file\n49999998 file\n\n$ time paste -sd+ file | bc\n1448700364\n\nreal    1m36.960s\nuser    1m24.515s\nsys     0m1.772s\n\n$ time awk '{s+=$1}END{print s}' file\n1448700364\n\nreal    0m45.476s\nuser    0m40.756s\nsys     0m0.287s\n\n3.\u5148cat\u7136\u540etr\u6700\u540e\u7ba1\u9053\u4f20\u7ed9bc\u547d\u4ee4\nYou can use bc (calculator). Assuming your file with #s is called \"n\":\n\n$ cat n\n1\n2\n3\n$ (cat n | tr \"\\012\" \"+\" ; echo \"0\") | bc\n6\nThe tr changes all newlines to \"+\"; then we append 0 after the last plus, then we pipe the expression (1+2+3+0) to the calculator\n\n4.Perl\u65b9\u6cd5\nOr, if you are OK with using awk or perl, here's a Perl one-liner:\n\n$perl -nle '$sum += $_ } END { print $sum' n\n6\n\n5.\u5c06\u5faa\u73af\u5199\u5728\u5355\u884c\nwhile read -r num; do ((sum += num)); done &lt; inputfile; echo $sum<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/450799\/shell-command-to-sum-integers-one-per-line\" target=\"_blank\">Shell command to sum integers, one per line?<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3096259\/bash-command-to-sum-a-column-of-numbers\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3096259\/bash-command-to-sum-a-column-of-numbers<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/450799\/shell-command-to-sum-integers-one-per-line\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/450799\/shell-command-to-sum-integers-one-per-line<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Stackoverflow\u4e0a\u770b\u5230\u7684\u4e00\u5219\u95ee\u7b54\uff0c\u4e4b\u524d\u6709\u6536\u96c6\u8fc7\u4e00\u4e9b\uff0c\u5728\u91cc\u9762\u90fd\u5305\u62ec\u4e86\uff0c\u8fd8\u6269\u5145\u4e86\u4e0d\u5c11\uff0c\u503c\u5f97\u5b66\u4e60\uff0c\u5c31\u6b64\u8bb0 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[74,8],"class_list":["post-1585","post","type-post","status-publish","format-standard","hentry","category-linux","category-tools","tag-awk","tag-python"],"views":4542,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1585"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1585\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}