APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2003/12/12 csplit

Split a file by context.

This again is a command I've seldom used. It's not that it isn't potentially useful, but usually the criteria I'd need are way beyond this commands capability. Apparently I'm not the only one to find little use for this: it's not even to be found in BSD distros.

Given the file "t":


"csplit t /ghi/" will produce two files (xx00 and xx01). The first will have the first two lines of "t" and the second will have the rest.

Probably more useful would be something like

csplit t /ghi/ {*}

which will give you four files, each beginning with "ghi" and containing lines up to but not including the next "ghi".

There can be more than one pattern:

csplit /ghi/ /def/ {*} 

gives the expected results (first two lines in first file, second file has three lines, etc.), and each pattern can have a repeat number:

csplit /ghi/  2 /def/ {*} 

While csplit can even get more fancy than this, I find it's usually easier to write a Perl program to get what I want.

Got something to add? Send me email.

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Software engineering is the part of computer science which is too difficult for the computer scientist. (Friedrich Bauer)

This post tagged: