约 5423 字

约 27 分钟

#Bash 的 csplit 命令

csplit [OPTION]... FILE PATTERN...

功能

将文件拆分为多个部分。

拆分后依次输出到文件 xx00,xx01, ...,并在标准输出上依次打印输出文件的字节数。

类型

可执行文件(/usr/bin/csplit),属于 coreutils。

参数

  • OPTION 选项:
    • -b, --suffix-format=FORMAT - 输出文件名的后缀格式
    • -f, --prefix=PREFIX - 输出文件名的前缀
    • -k, --keep-files - 产生错误时不要删除输出文件
    • --suppress-matched - 删除匹配的行
    • -n, --digits=DIGITS - 输出文件名的后缀数字位数
    • -s, --quiet, --silent - 不打印输出
    • -z, --elide-empty-files - 不输出空文件
    • --help - 显示帮助
    • --version - 显示版本
  • FILE - 要拆分的文件;- 表示读取标准输入
  • PATTERN - 模式列表,每个模式由以下结构组成:
    • 整数 - 复制到 INTEGER 行(不含该行)
    • /正则表达式/[行偏移量] - 复制到匹配的行(不含该行);行偏移量可以省略
    • %正则表达式%[行偏移量] - 跳过到匹配的行(不含该行);行偏移量可以省略
    • {整数} - 重复前面的模式 INTEGER 次
    • {*} - 重复前面的模式经可能多次

#示例

行号

$ man csplit | cat -n | csplit - 30 60
1012
1953
$ cat xx00                                          # xx00 从 0 到 29 行
...
    25	              do not remove output files on errors
    26
    27	       --suppress-matched
    28	              suppress the lines matching PATTERN
    29
$ cat xx01                                          # xx01 从 30 到 59 行
    30	       -n, --digits=DIGITS
    31	              use specified number of digits instead of 2
    32
    33	       -s, --quiet, --silent
...
    55	              repeat the previous pattern specified number of times
    56
    57	       {*}    repeat the previous pattern as many times as possible
    58
    59	       A line OFFSET is an integer optionally preceded by '+' or '-'
$ cat xx02                                          # xx02 从 60 行 到结尾
    60
    61	AUTHOR
    62	       Written by Stuart Kemp and David MacKenzie.
    63
    64	REPORTING BUGS
...

正则表达式

$ man csplit | csplit - '/^[A-Z]+$/' '{*}'          # 复制到匹配 /^[A-Z]+$/ 的行(不含该行),重复尽可能多次
80
77
52
1451
228
531
$ cat xx0                                           # 按每个小节拆分
CSPLIT(1)                        User Commands                       CSPLIT(1)

$ cat xx01                                      
NAME
       csplit - split a file into sections determined by context lines
$ cat xx02
SYNOPSIS
       csplit [OPTION]... FILE PATTERN...

$ cat xx03
DESCRIPTION
       Output  pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01',
       ..., and output byte counts of each piece to standard output.
...
$ cat xx04
AUTHOR
       Written by Stuart Kemp and David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

#推荐阅读

#手册

更新: 2026/1/18

作者: PlanC

创建: 2025/11/3