Splitting files with csplit


Periodically I find that I need to split a file into several pieces. There are a number of utilities that can be used to split a file, but I have grown to adore csplit. Csplit can split files based on regular expressions, or by quantity. The following example uses csplit to split /etc/services into a series of 10-line files:

$ csplit -f services.bak.00 /etc/services 10 {10}

172
208
225
372
408
188
450
392
256
325
434
521

$ ls -la service

-rw-r--r-- 1 root root 172 May 24 12:36 services.bak.0000
-rw-r--r-- 1 root root 208 May 24 12:36 services.bak.0001
-rw-r--r-- 1 root root 225 May 24 12:36 services.bak.0002
-rw-r--r-- 1 root root 372 May 24 12:36 services.bak.0003
-rw-r--r-- 1 root root 408 May 24 12:36 services.bak.0004
-rw-r--r-- 1 root root 188 May 24 12:36 services.bak.0005
-rw-r--r-- 1 root root 450 May 24 12:36 services.bak.0006
-rw-r--r-- 1 root root 392 May 24 12:36 services.bak.0007
-rw-r--r-- 1 root root 256 May 24 12:36 services.bak.0008
-rw-r--r-- 1 root root 325 May 24 12:36 services.bak.0009
-rw-r--r-- 1 root root 434 May 24 12:36 services.bak.0010
-rw-r--r-- 1 root root 521 May 24 12:36 services.bak.0011

Tis good stuff!

This article was posted by Matty on 2006-05-24 21:22:00 -0400 EDT