转置 CSV 文件的单列

转置 CSV 文件的单列

我有一个包含两列的 CSV 文件,即第一列:文件名第二列:访问状态

以下是一些示例记录

FileA, CREATE
FileA, MODIFY
FileA, DELETE
FileB, CREATE
FileB, MODIFY

我需要根据第一列的不同值将第二列的值转换为单行。

FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY

答案1

也试试

awk '
$1 != LAST      {printf "%s%s ", LD, $1         # print every new COL1 value
                 LAST = $1                      # and remeber it
                 LD = RS                        # set the line delimiter (empty at program start)
                 FD = ""                        # unset field delimiter
                }
                {printf "%s%s", FD, $2          # print successive second fields, after field delim 
                 FD = "|"                       # set the field delimiter
                }
END             {printf RS                      # last action: new line
                }
' file
FileA, CREATE|MODIFY|DELETE
FileB, CREATE|MODIFY

答案2

如果您不关心命令的顺序,可以使用:

$ awk -F"[, ]" '{
            a[$1][$2]++
           }
           END{
            for(i in a){
                printf "%s,",i; 
                for(k in a[i]){
                    printf  "%s|", k
                }
                print ""
                }
            }' file | sed 's/|$//'
FileA, DELETE|CREATE|MODIFY
FileB, CREATE|MODIFY

如果你需要这个顺序,你可以应用一些 Perl 魔法:

$ sed 's/ //' file | 
    perl -F, -lne 'push @{$k{$F[0]}},$F[1]; }{ 
    print "$_, ",join "|", @{$k{$_}} for keys(%k);' 
FileB, CREATE|MODIFY
FileA, CREATE|MODIFY|DELETE

答案3

awk '1 {if (a[$1]) {a[$1] = a[$1]" "$2"|"} else {a[$1] = $2"|"}} END {for (i in a) { print i,a[i]}}' file |sed 's/.$//'

答案4

使用 GNU awk 按排序顺序输出

gawk -F', ' '
    { a[$1] = a[$1] "|" $2 }
    END {
        PROCINFO["sorted_in"] = "@ind_str_asc"
        for (b in a) print b ", " substr(a[b], 2)
    }
'

要按按键的原始顺序输出:

awk -F', ' '
    !($1 in a) { keys[++count] = $1 }
    { a[$1] = a[$1] "|" $2 }
    END {
        for (i = 1; i <= count; i++)
            print keys[i] ", " substr(a[keys[i]], 2)
    }
'

相关内容