gpt4 book ai didi

linux,逗号分隔的单元格到行保留/聚合列

转载 作者:可可西里 更新时间:2023-11-01 11:50:40 26 4
gpt4 key购买 nike

这里有一个类似的问题,但对于 excel/vba Excel Macro - Comma Separated Cells to Rows Preserve/Aggregate Column因为我有一个大文件(> 300mb)这不是一个选项,因此我正在努力让它在 bash 中工作。

基于这些数据

 1   Cat1                 a,b,c
2 Cat2 d
3 Cat3 e
4 Cat4 f,g

我想将其转换为:

 1   Cat1                 a
1 Cat1 b
1 Cat1 c
2 Cat2 d
3 Cat3 e
4 Cat4 f
4 Cat4 g

最佳答案

cat > data << EOF
1 Cat1 a,b,c
2 Cat2 d
3 Cat3 e
4 Cat4 f,g
EOF

set -f # turn off globbing
IFS=, # prepare for comma-separated data
while IFS=$'\t' read C1 C2 C3; do # split columns at tabs
for X in $C3; do # split C3 at commas (due to IFS)
printf '%s\t%s\t%s\n' "$C1" "$C2" "$X"
done
done < data

关于linux,逗号分隔的单元格到行保留/聚合列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6103480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com