Skip to content

csplit

"split files based on context" - man csplit

There is a similar, simpler tool called split.

GNU Examples

GNU and BSD cpsplit are not compatible. In macOS you can use gcsplit if you have brew coreutils installed.

Split amazon RDS global certs into one cert per file

curl -fsSL https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem |
csplit --elide-empty-files --quiet --prefix global-rds-crt -k - '/-BEGIN CERTIFICATE-/' '{*}'

Split a multi-doc yaml file

This is great for splitting helm template or kubectl get pod,svc,sts,ds -o yaml output

$ wc -l k-get-all.yaml  # lots of lines in this one yaml file
9717 k-get-all.yaml

$ grep -c '^---$' k-get-all.yaml  # lots of docs too
161

$ csplit k-get-all.yaml -s --elide-empty-files --prefix=yaml-split- --suffix-format='%03d.yaml' '/^---$/' '{*}'

$ wc -l yaml-split-???.yaml
    38 yaml-split-000.yaml
    32 yaml-split-001.yaml
...long-list-of-files...
   227 yaml-split-159.yaml
   230 yaml-split-160.yaml
  9717 total