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":
abc def ghi jkl abc def abc def
"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.
More Articles by Tony Lawrence © 2011-07-06 Tony Lawrence
There is no reason anyone would want a computer in their home. (Ken Olson, president, chairman and founder of DEC)
Printer Friendly Version
csplit Copyright © December 2003 Tony Lawrence
Have you tried Searching this site?
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
Printer Friendly Version