> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/wc.1.md).

# wc.1

`wc` — 字、行、字符和字节计数

## 名称

`wc`

## 概要

`wc [--libxo] [-c | -m] [-Llw] [file ...]`

## 描述

`wc` 实用程序显示每个输入 `file` 中包含的行数、字数和字节数，若未指定文件则读取标准输入，并将结果写到标准输出。一行定义为由“换行”字符分隔的字符串。最后的“换行”字符之后的内容不计入行数。

一个字定义为由空白字符分隔的字符串。空白字符是 iswspace(3) 函数对其返回真的字符集合。如果指定了多个输入文件，则在最后一个文件的输出之后，单独一行显示所有文件的累计计数。

可用选项如下：

**`--libxo`** 通过 libxo(3) 以多种人类和机器可读格式生成输出。命令行参数的细节参见 xo\_options(7)。

**`-L`** 将包含最多字节（默认）或字符（指定 `-m` 时）的行的长度写到标准输出。当指定多个 `file` 参数时，*所有* 文件中最长的输入行作为最终“total”的值报告。

**`-c`** 将每个输入文件的字节数写到标准输出。此选项会抵消先前对 `-m` 选项的使用。

**`-l`** 将每个输入文件的行数写到标准输出。

**`-m`** 将每个输入文件的字符数写到标准输出。如果当前 locale 不支持多字节字符，则等价于 `-c` 选项。此选项会抵消先前对 `-c` 选项的使用。

**`-w`** 将每个输入文件的字数写到标准输出。

指定某个选项时，`wc` 仅报告该选项所请求的信息。输出的顺序始终为行、字、字节和文件名。默认行为等价于指定 `-c`、`-l` 和 `-w` 选项。

如果未指定文件，则使用标准输入且不显示文件名。提示会接受输入，直到收到 EOF，或在大多数环境中为 \[^D]。

如果 `wc` 收到 `SIGINFO` 信号（参见 stty(1) 的 `status` 参数），中间数据会以与标准完成消息相同的格式写到标准错误输出。

## 环境变量

`LANG`、`LC_ALL` 和 `LC_CTYPE` 环境变量按 [environ(7)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man7/environ.7.md) 中所述影响 `wc` 的执行。

## 退出状态

`wc` 实用程序成功时退出值为 0，发生错误时退出值大于 0。

## 实例

统计文件 `report1` 和 `report2` 各自的字符数、字数和行数，以及两者的总数：

```sh
wc -mlw report1 report2
```

在文件列表中查找最长的行：

```sh
wc -L file1 file2 file3 | fgrep total
```

## 兼容性

历史上，`wc` 实用程序的文档将“字”定义为“由 、 或 字符分隔的最大字符串”。然而，当时的实现未能正确处理非打印字符，因此“`\ \ ^D^E\ \` ”被计为 6 个空格，而“`foo^D^Ebar`”被计为 8 个字符。4.3BSD 之后的 4BSD 系统修改了实现以与文档保持一致。本实现按 iswspace(3) 函数定义“字”，如 IEEE Std 1003.2 ("POSIX.2") 所要求。

`-L` 选项是一个非标准 FreeBSD 扩展，与 GNU `wc` 实用程序的 `-L` 选项兼容。

## 参见

iswspace(3), libxo(3), xo\_options(7)

## 标准

`wc` 实用程序遵循 IEEE Std 1003.1-2001 ("POSIX.1")。

## 历史

`wc` 命令出现于 Version 1 AT\&T UNIX。


---

# 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://book.bsdcn.org/man/man1/wc.1.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.
