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

2003/12/12 csplit


© December 2003 Tony Lawrence

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.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> csplit


Inexpensive and informative Apple related e-books:

Photos: A Take Control Crash Course

Sierra: A Take Control Crash Course

Take Control of Apple Mail, Third Edition

Take Control of Parallels Desktop 12

Take Control of Upgrading to El Capitan




More Articles by © Tony Lawrence




Printer Friendly Version

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





There is no reason anyone would want a computer in their home. (Ken Olson, president, chairman and founder of DEC)




Linux posts

Troubleshooting posts


This post tagged:

Scripting

Shell

UnixWords



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode