使用AWK对csv文件中单个Key对应的多个值进行分组

使用AWK对csv文件中单个Key对应的多个值进行分组

在对与单个键对应的值进行分组时,我在具有两个以上字段的 csv 格式文件上尝试了此操作,例如:

Keyobject,FieldName,Fieldvalue
1,are you sleeping,yes
1,country of meeting,USA 
2,are you sleeping,' '
2,country of meeting,' '
3,are you sleeping,yes
3,country of meeting,CHINA
4,are you sleeping,yes
4,country of meeting,ITALY

预期输出:

Keyobject,Are you sleeping,country of meeting
1,yes,USA
2, ,
3,yes,CHINA
4,no,ITALY.

请提供粗略的想法或任何脚本。

答案1

awk -F, '
    NR > 1 {ids[$1]; keys[$2]; value[$1,$2] = $3}
    END { 
        printf "Keyobject"
        for (k in keys)
            printf ",%s", k
        print ""

        for (id in ids) {
            printf id
            for (k in keys)
                printf ",%s", value[id,k]
            print ""
        }
    }
' file

答案2

也试试

awk -F, -v OFS="," '
NR == 1                 {HD      = "Keyobject,are you sleeping,country of meeting"
                         for (MX=n=split (HD, HDArr, OFS); n>0; n--) SRCH[HDArr[n]]
                         print HD
                         next
                        }


$2 in SRCH              {RES[$2] = $3
                         RES[HDArr[1]] = $1
                        }

NR%2                    {for (i=1; i<=MX; i++) printf "%s%s", RES[HDArr[i]], (i == MX)?ORS:OFS
                         split ("", RES)
                        }

' file
Keyobject,are you sleeping,country of meeting
1,yes,USA 
2,' ',' '
3,yes,CHINA
4,yes,ITALY

读取第 1 行时,将创建标题和由标题元素索引的搜索数组,并打印标题。在下面的行中,由 $2 索引的结果数组被分配给 $3,并且对于“Keyobject”索引字符串来说是冗余的。每隔一行,打印结果,并删除结果数组。

答案3

awk <file -v IV="' '" -v OV=" " -F, '
        $1+0 {
                b = $2 == IV ? OV : $2
                a[$1,$2] = $3 == IV ? OV : $3
                if($1 > max) max = $1; t[$2]++;
        }
        END{
                ORS = ""
                print "Keyobject"
                for(k in t) print FS k
                print RS
                for(i = 1; i <= max; i++){
                        print i
                        for(k in t)
                                print FS (i SUBSEP k in a ? a[i,k] : OV)
                        print RS
                }
        }
'

分别调整IVOV变量以确定输入和输出中的空字段应包含的内容。

答案4

假设您的 csv 文件名为“list.csv”。我会这样做:

#!/bin/bash
echo -e "Keyobject","Are you sleeping ", " Country of meeting"   # create new header
cat list.csv | sed 1,1d | awk -F, 'NF>1{print $NF}' | paste -d', ' - - | sed 's/ /,/' | nl


 1  yes,USA 
 2  ' ', ' '
 3  yes,CHINA
 4  yes,ITALY

解释:

sed 1,1d # This command excludes the original header in list.csv
awk -F, 'NF>1{print $NF}' # This command reads the last word in each row
paste -d', ' - -  # This command paste the words beside each other 
sed 's/ /,/'  # This command replaces blank spaces with commas
nl # This command adds numbering to the rows in the final output

但是,此解决方案假定原始 csv 文件中的行的组织方式如上面的示例所示。

相关内容