In need of text splitter
April 9, 2012 5:15 PM   Subscribe

Is there a text editor under Mac OS X which can split a plain text file using a string as delimiter?

There are programs under Windows that have this capability. And I have come upon some AppleScript and bash scripts which seem capable of doing this, but I have not found this function offered within a program.
posted by yclipse to Computers & Internet (19 answers total) 1 user marked this as a favorite
It's pretty much guaranteed that the command-line tools in OSX can do what you want, but you'll need to tell us more about what you mean by "split".
posted by mhoye at 5:28 PM on April 9, 2012

for example vim
posted by hni at 5:34 PM on April 9, 2012

TextWrangler can probably do it with a regex-based replace.
posted by lefty lucky cat at 5:36 PM on April 9, 2012

I thought macs came with some version of VIM already installed. It can probably do what you want.
posted by DarkForest at 5:47 PM on April 9, 2012

A trick that I've used in the past that works in some programs, maybe it'll be useful:

You want to copy a single line feed onto the clipboard. Go to the end of a line in the program, hold down the Shift key and press right-arrow (this should move the cursor to the beginning of the next line). Release Shift then press Command-C (Ctrl-C in Windows). Then, go to the Replace form. Enter the text you want to delimit by in the Find field, and in the Replace field, press Command-V (Ctrl-V in Windows). Do a single replace first to see if it works correctly.
posted by JHarris at 6:09 PM on April 9, 2012

OS X comes with emacs. Very powerful editor (can do what you want easily) but might take you a long time to get proficient.
posted by ill3 at 6:10 PM on April 9, 2012

(It's best to do this on a backup copy, or be sure not to save afterward if it doesn't work.)
posted by JHarris at 6:10 PM on April 9, 2012

Response by poster: By "split", I mean take a 100 kb text file and split it into four 25 kb files.
posted by yclipse at 6:23 PM on April 9, 2012

there's a command line program for that. 'man split' for details.
posted by scruss at 6:39 PM on April 9, 2012 [2 favorites]

Response by poster: (better stated) . . . take the 100 kb text file and split it into 4 or 12 smaller files that are demarcated at a given string delimiter.
posted by yclipse at 7:20 PM on April 9, 2012

Best answer: You may actually want csplit:
       csplit - split a file into sections determined by context lines

       csplit [OPTION]... FILE PATTERN...

       Output pieces of FILE separated by PATTERN(s) to files `xx00', `xx01',
       ..., and output byte counts of each piece to standard output.
posted by brennen at 8:08 PM on April 9, 2012

Best answer: split -p '^plop' file.txt out

will split file.txt whenever a line starts with 'plop', into files outa, outb, outc, ...
posted by scruss at 8:08 PM on April 9, 2012

Both vim and emacs can pipe selected parts of the current edit buffer through any external program you like, meaning that scruss's split solution can be used inside either.
posted by flabdablet at 8:11 AM on April 10, 2012

To add a bit of clarity here, BSD split(1) has the -p feature. It doesn't look like GNU split(1) does, at least not in the wild on the Linux machine I was using.
posted by brennen at 1:44 PM on April 10, 2012

That's right, and it's the BSD version you'll find installed by default on OS X.

For doing the same job with the GNU tools typical of Linux boxes, there's csplit.
posted by flabdablet at 7:24 PM on April 10, 2012

Response by poster: Got split to work (after trepidation opening the Terminal window). Didn't get csplit to work, probably because I left off the {*} as repeat marker. Will keep at it. Thanks to all.
posted by yclipse at 4:21 AM on April 11, 2012

On OS X you will probably find that you have BSD csplit, not the GNU version whose man page I linked to earlier.

There are numerous differences between the BSD tools and their nominal GNU equivalents, and making sure you're reading the documentation for the version you actually have will save confusion and swearing. The definitive reference is the manual pages supplied with your OS; opening a Terminal window and typing man csplit will get you there.
posted by flabdablet at 10:31 PM on April 11, 2012

Response by poster: As a Windows user, too, I blanch at anything called BSD. But I'll check it out.
posted by yclipse at 2:41 PM on April 12, 2012

BSD ain't BSoD :-)
posted by flabdablet at 6:48 PM on April 12, 2012

« Older Are you a reformed malingerer? How did you quit?   |   Sync fuel records across multiple iThings? Newer »
This thread is closed to new comments.