Lesser known linux commands – csplit

csplit – context split – is a command to split a text file into smaller files based on some context. Let me explain with an example. I have a file  csplit.txt, like this

ram@rhlaptop:~/blog$ cat csplit.txt
Chapter 1
This is chapter one
Chapter 2
This is chapter two
Chapter 3
This is chapter three

I want this file to be broken into three pieces – chapter wise. I can use csplit like this

ram@rhlaptop:~/blog$ csplit -z csplit.txt  /Chapter/ {*}
31
31
32

Let me explain the command. –

csplit.txt – name of the input file

-z remove any empty output file

/Chapter/ : Indicate the match string

{*} : indicates number of times. If star means any number of times. You can give any integer to  limit number of splits.

Upon running the above command it creates three files named xx00,xx01,xx02.  whose contents are

ram@rhlaptop:~/blog$ cat xx00
Chapter 1
This is chapter one

ram@rhlaptop:~/blog$ cat xx01
Chapter 2
This is chapter two

ram@rhlaptop:~/blog$ cat xx02
Chapter 3
This is chapter three

csplit is thus very simple and yet very poweful. It will be very useful when we want to split a large file like a book into chapters.

csplit has options for specifying output file name, regular expression for pattern or simply number of lines. csplit is an improvement over split command.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: