#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/>
#推荐阅读
#手册
CSPLIT(1) User Commands CSPLIT(1) NAME csplit - split a file into sections determined by context lines SYNOPSIS csplit [OPTION]... FILE PATTERN... DESCRIPTION Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output. Read standard input if FILE is - Mandatory arguments to long options are mandatory for short options too. -b, --suffix-format=FORMAT use sprintf FORMAT instead of %02d -f, --prefix=PREFIX use PREFIX instead of 'xx' -k, --keep-files do not remove output files on errors --suppress-matched suppress the lines matching PATTERN -n, --digits=DIGITS use specified number of digits instead of 2 -s, --quiet, --silent do not print counts of output file sizes -z, --elide-empty-files suppress empty output files --help display this help and exit --version output version information and exit Each PATTERN may be: INTEGER copy up to but not including specified line number /REGEXP/[OFFSET] copy up to but not including a matching line %REGEXP%[OFFSET] skip to, but not including a matching line {INTEGER} repeat the previous pattern specified number of times {*} repeat the previous pattern as many times as possible A line OFFSET is an integer optionally preceded by '+' or '-' 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/> COPYRIGHT Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO Full documentation <https://www.gnu.org/software/coreutils/csplit> or available locally via: info '(coreutils) csplit invocation' GNU coreutils 9.4 April 2024 CSPLIT(1)