首頁·終端機命令

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