Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at the csplit command. It’s a command that lets you split a file into pieces based on its content. For example, you might separate a file based on chapter headings, dates or content separators of some kind. Think of “csplit” as meaning “context split”.
If you have a text file separated by lines like “Part 1” and “Part 2”, you could split it into a series of files (each containing a single Part) with a command like this:
This will split the file specified (myreport) on lines that begin with the word “Part” and name them Part-00, Part-01 and so on. We see the size of each file as the command creates them.
The -b option
Let’s look at the resulting files:
In this case, the first file is empty, but each of the others contains the contents of one of the 5 parts and is named to reflect what it contains. There are many ways to use the csplit command to break files into meaningful pieces. I hope this example gives you an idea how it works.
That’s your Linux tip for the csplit command.
If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube.
If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.