3

Suppose I have:

cat file
"Field 1, Line 1",Field 2,"Field 3, Line 1"
"Field 1, Line 2",Field 2,"Field 3, Line 2"

With Miller, I want to produce:

"Field 1, Line 1"
"Field 2"
"Field 3, Line 1"
"Field 1, Line 2"
"Field 2"
"Field 3, Line 2"

I tried this:

mlr --csv -N  --ofs lf --quote-all cat file

Which produces no output.

As a workaround, I can do this:

mlr --csv -N  --ofs pipe --quote-all cat file | sed 's/"\|"/"\n"/g'

Or use ruby:

ruby -r csv -e 'CSV.parse($<.read).flatten.each{|r| puts [r].to_csv(force_quotes: true)}' file

But it does feel that I should be able to just look at a csv file one field at a time (and ignore records) with Miller?

dawg
  • 98,345
  • 23
  • 131
  • 206

2 Answers2

4

You can reshape and enumerate:

mlr --csv --quote-all -N reshape -r ".*" -o a,b  input.csv
"1","Field 1, Line 1"
"2","Field 2"
"3","Field 3, Line 1"
"1","Field 1, Line 2"
"2","Field 2"
"3","Field 3, Line 2"

Then cut the first column (cut -f b keeps the second column named b in this case):

mlr --csv --quote-all -N reshape -r ".*" -o a,b then cut -f b input.csv

to get:

"Field 1, Line 1"
"Field 2"
"Field 3, Line 1"
"Field 1, Line 2"
"Field 2"
"Field 3, Line 2"
dawg
  • 98,345
  • 23
  • 131
  • 206
aborruso
  • 4,938
  • 3
  • 23
  • 40
1

While Miller treats --ifs and --ofs differently for the CSV format, not outputting any error message with --ofs lf feels like a bug...

As a workaround I would split the input records into multiple single-fielded ones:

mlr -c -N --quote-all filter -q 'for (k,v in $*) {emit {1:v}}'
"Field 1, Line 1"
"Field 2"
"Field 3, Line 1"
"Field 1, Line 2"
"Field 2"
"Field 3, Line 2"
Fravadona
  • 13,917
  • 1
  • 23
  • 35