Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Is there a robust command line tool for processing csv files?

I can use cut to extract columns from a file:

$ cat foo

$ cut -d\, -f2 foo

But what if a column has a delimiter in it that is safe inside an enclosure like this?

$ cat foo
foo,"hello, world",bar

$ cut -d\, -f2 temp12

Is there some way to tell cut to respect the enclosure of quote "" characters?

share|improve this question

marked as duplicate by Gilles, jasonwryan, Renan, derobert, Mat Nov 28 '12 at 6:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Strictly speaking, the answer is: no, you need a proper CSV parser. – Gilles Nov 27 '12 at 0:13
You could quickly code up csv output script using some python or perl. – sparticvs Nov 27 '12 at 3:06
up vote 1 down vote accepted

cut could do it if you first preprocess its input to escape the characters inside the quotes (for instance, replace "_" with "_u" and "," with "_c" inside quotes, or replace every character with their 2-byte hex notation) and postprocess it to restore that escaping.

Something like:

perl -pe 's/"(.*?)"/"\"".unpack("H*", $1)."\""/ge' |
  cut -d, -f2 |
  perl -pe 's/"(.*?)"/"\"".pack("H*",$1)."\""/ge'

(assuming there's no escaped quotes inside (or outside) the quotes).

But, given the effort it would require, you might as well use a proper csv parser or do it whole with a perl-like regular expression engine.

share|improve this answer
I don't think this is the solution he should be looking for. – Bernhard Nov 27 '12 at 15:50

Definitely not for GNU cut, at least:

/* The delimeter character for field mode. */ static unsigned char delim;

(as seen in GNU coreutils source)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.