如何使用 csvkit 将 csv 格式化为多行换行分隔的纯文本输出
How to format csv into multiline newline delimited plain text output using csvkit
我有一个这样的 csv,
type,name,ad1,pin,ph
"A","aaaaa","23 rd.","45789","4578954"
"F","bbbbb","23 rd.","84789","4578954"
"D","ccccc","34 rd.","45646","7845663"
这需要格式化为这样的纯文本文件。
type
name
ad1, PIN-pin
PH: ph
所以最后的输出应该是这样的
A
aaaaa
23 rd., PIN- 45789,
PH: 4578954
F
bbbbb
23 rd. PIN-84789
PH:4578954
D
ccccc
34 rd., PIN-45646
PH: 7845663
有没有可能在csvkit中实现这个。
您可以使用米勒 (https://github.com/johnkerl/miller/releases/tag/5.4.0):
mlr --c2x --ops "\t" put '=$type;=$name;=($ad1 . ", PIN-" . $pin);=("PH: " . $ph)' \
then cut -r -f "^[0-9]" input.csv | \
cut -f2
有
A
aaaaa
23 rd., PIN-45789
PH: 4578954
F
bbbbb
23 rd., PIN-84789
PH: 4578954
D
ccccc
34 rd., PIN-45646
PH: 7845663
一些注意事项:
--c2x
将 csv 转换为 XTAB (http://johnkerl.org/miller/doc/file-formats.html#XTAB:_Vertical_tabular);
--ops
将制表符设置为对分隔符
put
以您想要的方式设置字段(我创建了 4 个新字段,分别命名为 1、2、3 和 4)
cut
(管道前)删除除 1、2、3 和 4 之外的所有字段
- 最后
cut
删除字段名
我有一个这样的 csv,
type,name,ad1,pin,ph
"A","aaaaa","23 rd.","45789","4578954"
"F","bbbbb","23 rd.","84789","4578954"
"D","ccccc","34 rd.","45646","7845663"
这需要格式化为这样的纯文本文件。
type
name
ad1, PIN-pin
PH: ph
所以最后的输出应该是这样的
A
aaaaa
23 rd., PIN- 45789,
PH: 4578954
F
bbbbb
23 rd. PIN-84789
PH:4578954
D
ccccc
34 rd., PIN-45646
PH: 7845663
有没有可能在csvkit中实现这个。
您可以使用米勒 (https://github.com/johnkerl/miller/releases/tag/5.4.0):
mlr --c2x --ops "\t" put '=$type;=$name;=($ad1 . ", PIN-" . $pin);=("PH: " . $ph)' \
then cut -r -f "^[0-9]" input.csv | \
cut -f2
有
A
aaaaa
23 rd., PIN-45789
PH: 4578954
F
bbbbb
23 rd., PIN-84789
PH: 4578954
D
ccccc
34 rd., PIN-45646
PH: 7845663
一些注意事项:
--c2x
将 csv 转换为 XTAB (http://johnkerl.org/miller/doc/file-formats.html#XTAB:_Vertical_tabular);--ops
将制表符设置为对分隔符put
以您想要的方式设置字段(我创建了 4 个新字段,分别命名为 1、2、3 和 4)cut
(管道前)删除除 1、2、3 和 4 之外的所有字段- 最后
cut
删除字段名