csv 米勒:ofs="\n”?

cwtwac6a  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(131)

假设我有:

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

与米勒,我想生产:

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

我试过这个:

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

它不产生输出。
作为一种变通方法,我可以这样做:

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

或者使用ruby:

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

但它确实觉得我应该能够只看一个csv文件一个字段的时间(并忽略记录)与米勒?

btqmn9zl

btqmn9zl1#

您可以重塑和枚举:

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"

然后剪切第一列(在本例中,cut -f b保留第二列b):

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

获得:

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

wd2eg0qa2#

虽然米勒对--ifs--ofs的CSV格式处理不同,但--ofs lf不输出任何错误消息感觉像是一个bug。
作为一种变通方法,我将输入记录拆分为多个单字段记录:

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"

相关问题