【问题标题】:How to split the values of the particular column from the csv file using unix command如何使用 unix 命令从 csv 文件中拆分特定列的值
【发布时间】:2020-12-10 01:13:51
【问题描述】:

我拥有的数据

item     ids
358159   419463_I,528691_I,618536_I,768347_I,794716_I,802503_I,850094_I

358944   090169_I,398844_I,698490_I

我需要的结果

item      ids
358159   419463_I
358159   528691_I
358159   618536_I
358159   768347_I
358159   794716_I
358159   802503_I
358159   850094_I
358944   090169_I
358944   398844_I

能否使用 unix 命令 awk 或 sed 对其进行格式化

【问题讨论】:

标签: linux shell unix sh


【解决方案1】:
awk -F[," "] 'NR==1 { printf "%s\t%s\n","itm","ids" } NR>1 { itm=$1;for (i=4;i<=NF;i++) { if ($NF!=" ") { print itm" "$i } } }' file

使用 awk,将字段分隔符设置为空格或逗号。当遇到第一条记录时,打印标题,否则,循环遍历字段,记下第一个字段 (itm) 并将其与字段一起打印在单独的行中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2015-04-29
    • 2018-11-04
    • 2018-04-01
    相关资源
    最近更新 更多