csplit

file processingLinux/Unix
The csplit command is one of the most frequently used commands in Linux/Unix-like operating systems. csplit Split a file into sections determined by context lines

Quick Reference

Command Name:

csplit

Category:

file processing

Platform:

Linux/Unix

Basic Usage:

csplit [options] [arguments]

Common Use Cases

  • 1

    Log file processing

    Split large log files into sections by date or event markers

  • 2

    Content extraction

    Extract specific sections from structured documents

  • 3

    Data processing

    Break large data files into manageable chunks for processing

  • 4

    Document segmentation

    Divide documents by chapters, sections, or other logical units

Syntax

csplit [OPTION]... FILE PATTERN...

Options

Option Description
-f, --prefix=PREFIX Use PREFIX instead of 'xx' for output files
-b, --suffix-format=FORMAT Use sprintf FORMAT instead of '%02d' for output file names
-n, --digits=DIGITS Use specified number of digits instead of 2
-k, --keep-files Do not remove output files on errors
-s, --quiet, --silent Do not print counts of output file sizes
-z, --elide-empty-files Remove empty output files
--suppress-matched Suppress the lines matching PATTERN
-F, --filter=SCRIPT Write to shell SCRIPT; filenames are $FILE variables
--help Display help and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the csplit command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

# Split a file at line 100
csplit large_file.txt 100
# Split a file at lines 100, 200, and 300 csplit large_file.txt 100 200 300
# Split a file before lines matching a pattern csplit document.txt '/CHAPTER/' '{*}'
# Split a file before each line containing "SECTION" csplit report.txt '%SECTION%' '{*}'

Advanced Examples:

# Split a file at every occurrence of "CHAPTER" after line 10
csplit book.txt 10 '/CHAPTER/' '{*}'
# Split a file into multiple files with custom prefix and suffix csplit --prefix=chapter_ --suffix-format="%03d.txt" book.txt '/CHAPTER/' '{*}'
# Split a file and get only specific sections (sections 2-4) csplit --suppress-matched --prefix=section_ document.txt '/SECTION/' '{2}' # Split a file with a custom number of digits in output filenames csplit --digits=3 large_file.txt 1000 2000 3000 # Split a CSV file after header and before each record starting with "2023" csplit -z data.csv 1 '/^2023/' '{*}' # Split a file with repeat pattern but keep the matched lines in the output csplit --suppress-matched=0 log_file.txt '/^Date:/' '{*}' # Split a file but keep only files matching a specific size csplit -b '%d.part' --elide-empty-files large_file.txt 1000 2000 3000

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The csplit command is used to split a file into multiple sections based on line numbers or patterns. It's particularly useful for breaking large text files into manageable chunks or for extracting specific sections from structured text files.

Key points about csplit:

  • Output files are named by default with the prefix 'xx' followed by a number (xx00, xx01, etc.)
  • You can specify custom prefixes and formatting for output filenames
  • It can split files based on line numbers, patterns, or a combination of both
  • The {N} syntax indicates repetition of the previous pattern N times
  • {*} means to repeat as many times as possible
  • By default, the matching pattern line is included at the beginning of the new file
  • Use --suppress-matched to exclude the pattern line from the output

Pattern specifications:

  • N: Split after line number N
  • /PATTERN/: Split before lines containing PATTERN
  • %PATTERN%: Split before lines containing PATTERN
  • {N}: Repeat the previous pattern N times
  • {*}: Repeat the previous pattern as many times as possible

Common use cases:

  • Splitting large log files by date entries
  • Extracting chapters from book manuscripts
  • Breaking down CSV files into smaller batches
  • Separating sections of configuration files
  • Dividing source code files by function or class definitions
  • Extracting specific parts of structured text documents

Tips when using csplit:

  • Use the -s (silent) option to suppress output messages in scripts
  • The -z option is useful to avoid creating empty files when patterns don't match
  • Remember that line numbers are relative to the current position when used after a pattern
  • Check the output filenames to ensure all expected sections were created
  • When working with very large files, consider using --prefix to create more meaningful filenames
  • To split a file into equal-sized chunks regardless of content, use the split command instead

Tips & Tricks

1

Use the -f prefix option to specify the prefix for output files

2

Use the -n number option to specify the number of output files

3

Use the -k option to keep the original file unchanged

4

Use the -s option to suppress the output of split file sizes

5

Use the -z option to split on null bytes instead of newlines

Common Use Cases

Log file processing

Split large log files into sections by date or event markers

Content extraction

Extract specific sections from structured documents

Data processing

Break large data files into manageable chunks for processing

Document segmentation

Divide documents by chapters, sections, or other logical units

Code analysis

Split source code files by function or class definitions

Related Commands

These commands are frequently used alongside csplit or serve similar purposes:

Use Cases

1

Log file processing

Split large log files into sections by date or event markers

2

Content extraction

Extract specific sections from structured documents

3

Data processing

Break large data files into manageable chunks for processing

4

Document segmentation

Divide documents by chapters, sections, or other logical units

5

Code analysis

Split source code files by function or class definitions

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the csplit command works in different scenarios.

$ csplit
View All Commands