Report issue Add example

csplit

Split a large file into smaller pieces based on context

Description

The csplit command is used to split a large file into smaller fragments and save each fragment as a separate file. The fragment files are named "xx00", "xx01", etc., by default. csplit is a variant of the split command; while split can only split files based on size or line count, csplit can split files based on specific patterns or context within the file.

Syntax

csplit [options] [arguments]

Options

-b <format> or --suffix-format=<format>: Specify the format for the filename suffix (default is %02d).
-f <prefix> or --prefix=<prefix>: Specify the prefix for the output filenames (default is "xx").
-k or --keep-files: Do not remove output files even if an error occurs.
-n <digits> or --digits=<digits>: Specify the number of digits in the filename suffix.
-q, -s, --quiet, --silent: Do not print the size of the output files.
-z or --elide-empty-files: Remove output files that are zero bytes in length.

Arguments

Examples

Example source file server.log:

cat server.log
SERVER-1
[con] 10.10.10.1 suc
[con] 10.10.10.2 fai
[dis] 10.10.10.3 pen
[con] 10.10.10.4 suc
SERVER-2
[con] 10.10.10.5 suc
[con] 10.10.10.6 fai
[dis] 10.10.10.7 pen
[con] 10.10.10.8 suc
SERVER-3
[con] 10.10.10.9 suc
[con] 10.10.10.10 fai
[dis] 10.10.10.11 pen
[con] 10.10.10.12 suc

To split server.log into server01.log, server02.log, and server03.log based on the SERVER sections:

[root@localhost split]# csplit server.log /SERVER/ -n2 -s {*} -f server -b "%02d.log"; rm server00.log
[root@localhost split]# ls
server01.log  server02.log  server03.log  server.log

Command Explanation:

/[regex]/   # Matches a pattern, e.g., /SERVER/. Splits from the current line to the line matching the pattern.
{*}         # Repeat the split until the end of the file. You can also specify an integer for the number of splits.
-s          # Silent mode; do not print file sizes.
-n          # Number of digits for the suffix (e.g., 01, 02, 03).
-f          # Prefix for the output filenames.
-b          # Suffix format (e.g., %02d.log, similar to printf format in C).
rm server00.log    # Deletes the first file, which is empty because the first match occurs on the first line.

观察