Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
foo,bar,hello
bash,baz,world

$ cut -d\, -f2 foo
bar
baz

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

$ cat foo
foo,"hello, world",bar
bash,goodbye,baz

$ cut -d\, -f2 temp12
"hello
goodbye

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

share|improve this question
3  
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
add comment

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.

2 Answers

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
add comment

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
add comment

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