csvmiller

Miller: ofs="\n"?


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?


Solution

  • 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"