首页·终端命令

csplit

文本处理

用法

csplit [file] '/[pattern]/' '{*}'

按模式出现的位置切文件,而不是按固定大小;它打印的数字是每块的字节数,{*} 表示匹配多少次就重复多少次。

常用选项

选项含义
-fPrefix for the output names; the default is xx
-nHow many digits the number has
-kKeep the files already written if it fails partway
-sDo not print the byte count of each piece
-zGNU: throw away pieces that came out empty
{*}Repeat the pattern as many times as it matches

示例

csplit -z -f part_ log.txt '/^ERROR/' '{*}'

A new file at every ERROR line.

csplit book.txt 100 200

Splits at lines 100 and 200.

它们本来就是用管道串起来用的。串三条往往比让一条命令做完所有事更短。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. csplit 是做什么的?

按模式出现的位置切文件,而不是按固定大小;它打印的数字是每块的字节数,{*} 表示匹配多少次就重复多少次。

Q. 怎么写?

csplit [file] '/[pattern]/' '{*}' —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man csplit。这条命令属于文本处理。

相关命令

man 手册: man csplit