|
|
|
SearchCategories
Books by the AuthorOther Ruby Projects |
The Secret Shell Helper
Posted 2 months ago
in FasterCSV and The Standard Library.
Someone pops onto the Ruby Talk mailing list fairly regularly asking how to break up content like:
They expect to end with a three element If your needs are very, very simple you may be able to handle this with a regular expression:
That just searches for either a set of quotes with some non-quote characters between them or a run of non-whitespace characters. Those are the two possibilities for the fields. Note that the two separate capture here mean
That's why I added a The regular expression approach can get pretty complex though if any kind of escaping for quotes is involved though. When that happens, you may need to step up to a parser. One choice for that would be to abuse a CSV parser to get it to divide up the data for you. Here's how you would do that with
As you see, replacing the column separator (traditionally a comma) with a simple space gets This parser will handle escaping, though it's CSV style escaping. That means that quotes will need to be doubled:
I doubt that fits the data well too often though. I suspect that an escaped quote is more often I'm guessing it's shell data more often that not. Most shells handle quoting like this:
If that's really the case, we're going to need a shell oriented parser. It's sadly not well known, I assume because it's strangely absent from http://ruby-doc.org/, but Ruby ships with such a parser. The standard
If your data really is shell content, you'll be glad to know that
Hopefully this helps you find the right parser for your data. |
|
|
|