Splitting text files

If you’ve got a big text document you’d like split into many smaller files, you can accomplish this with the csplit shell tool. Unfortunately, if you don’t speak Unix-ese fluently, the meager documentation isn’t going to help figure out to use this effectively (or many of the bash tools, for that matter). For documentation in plain English, get yourself a good book such as Learning the Bash Shell, from O’Reilly. Meanwhile, here’s a brief on splitting files. csplit has many options, or flags for achieving precise control. You give these to csplit while entering your command, which in turn affects the format of the output. Let’s take an example. Say you’ve got a report with many chapters, all as a single doc, and now you want to split each chapte off into a separate file. Assume that each chapter begins with the text, „Chapter:“. And assume you want every resulting filename to start with „doc.“ The existing file is called Report. Here’s your command: csplit -f doc Report /Chapter:/ {*} The -f flag specifies the prefix, so that every file gets named doc1, doc2, etc. You could give more arguments here, such as -n 3 to specify that numbering takes place in a three-digit scheme. Then comes the input filename, then the string to be searched on, surrounded by /s . Finally, the {*} specifies that csplit should keep on doing its thing until it runs out of instances of the text string.


Comments: 1


Great tip! It did exactly what I wanted.



(comments are closed)