> 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/cut.1.md).

# cut.1

`cut` — 截取文件每一行中选定的部分

## 名称

`cut`

## 概要

`cut -b list [-n] [file]`

`cut -c list [file]`

`cut -f list [-w | -d delim] [-s] [file]`

## 描述

`cut` 实用程序从每个 `file` 中截取每一行选定的部分（由 `list` 指定），并将其写入标准输出。如果未指定 `file` 参数，或者文件参数为单个短横线（“`-`”），`cut` 从标准输入读取。`list` 指定的项可以按列位置，也可以按由特定字符分隔的字段。列和字段的编号从 1 开始。

`list` 选项参数是一个以逗号或空白字符分隔的递增数字和/或数字范围的集合。数字范围由一个数字、一个短横线（“-”）和第二个数字组成，选取从第一个数字到第二个数字（含）的列或字段。数字或数字范围前可以加一个短横线，选取从 1 到该数字的所有列或字段。数字或数字范围后可以跟一个短横线，选取从该数字到行尾的所有列或字段。数字和数字范围可以重复、重叠，且顺序任意。选取输入行中不存在的列或字段不算错误。

选项如下：

**`-b`** `list` `list` 指定字节位置。

**`-c`** `list` `list` 指定字符位置。

**`-d`** `delim` 使用 `delim` 作为字段分隔符，而不是制表符。

**`-f`** `list` `list` 指定字段，输入中由字段分隔符分隔（参见 `-d` 选项）。输出字段以单个字段分隔符分隔。

**`-n`** 不拆分多字节字符。仅当至少选中了一个字节时才输出字符，并且在零个或多个未选中的字节前缀之后，组成该字符的其余字节会被选中。

**`-s`** 抑制不包含字段分隔符的行。除非指定此选项，否则不含分隔符的行将原样输出。

**`-w`** 使用空白字符（空格和制表符）作为分隔符。连续的空格和制表符视为单个字段分隔符。

## 环境变量

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

## 退出状态

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

## 实例

从系统 [passwd(5)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man5/passwd.5.md) 文件中提取用户的登录名和 shell，并以“name:shell”对的形式输出：

```sh
cut -d : -f 1,7 /etc/passwd
```

显示当前已登录用户的名称和登录时间：

```sh
who | cut -c 1-16,26-38
```

## 参见

[colrm(1)](/man/man1/colrm.1.md), [paste(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/paste.1.md)

## 标准

`cut` 实用程序遵循 ISO/IEC 9945-2:1993 ("POSIX.2") 规范。

`-w` 标志是对该规范的扩展。

## 历史

`cut` 命令首次出现于 4.3BSD-Reno。

## 作者

最初的 Bell Labs 版本由 Gottfried W. R. Luderer 编写，BSD 版本由 Adam S. Moskowitz 编写。


---

# 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/cut.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.
