> For the complete documentation index, see [llms.txt](https://huataihuang.gitbook.io/cloud-atlas-draft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://huataihuang.gitbook.io/cloud-atlas-draft/develop/shell/sed_awk/sum_all_rows_of_all_columns.md).

# 累加所有行的每一列数值

最近的一个计算案例，类似如下

```
1 4 7 ...
2 5 8
3 6 9 
```

需要计算出每一列的累加值

```
6 15 24 ...
```

巧妙的方法:

```bash
awk '{for(i=1;i<=NF;i++)$i=(a[i]+=$i)}END{print}' file
```

解释:

* `{for (i=1;i<=NF;i++)` 表示数值字段从1 到 最后一列
* `$i=(a[i]+=$i)` 表示将每列叠加
* `END{print}` 最后打印

## 参考

* [Sum of all rows of all columns - Bash](https://stackoverflow.com/questions/27104990/sum-of-all-rows-of-all-columns-bash)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/shell/sed_awk/sum_all_rows_of_all_columns.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
