這是一個基於GNU awk的解決方案。我強調這一點,因爲多維數組(這是爲了方便的解決方案而獲得的)是GNU awk特有的特性。
我的腳本table2matrix.awk
:
# collect values
{
# category=$1 ; type=$2 ; value=$3
if (!($1 in categories)) { categories[$1] }
types[$2][$1] = $3
}
# output of values
END {
# print col. header
for (category in categories) { printf("\t%s", category); }
print ""
# print rows
for (type in types) {
printf("%s", type);
for (category in categories) {
printf("\t%s", types[type][category]);
}
print ""
}
}
樣品會話:
$ cat >table.txt <<EOF
> Category1 type1 +
> Category1 type2 -
> Category1 type3 +
> Category2 type1 +
> Category2 type2 +
> Category2 type3 +
> Category3 type1 +
> Category3 type2 -
> Category3 type3 -
> EOF
$ awk -f table2matrix.awk table.txt
Category1 Category2 Category3
type1 + + +
type2 - + -
type3 + + -
$ cat table.txt | sed $'s/ /\t/g' >table-tabs.txt
$ awk -f table2matrix.awk table-tabs.txt
Category1 Category2 Category3
type1 + + +
type2 - + -
type3 + + -
$ cat >table-sorted.txt <<EOF
> Category1 type1 +
> Category1 type3 +
> Category2 type1 +
> Category2 type2 +
> Category2 type3 +
> Category3 type1 +
> Category1 type2 -
> Category3 type2 -
> Category3 type3 -
> EOF
$ awk -f table2matrix.awk table-sorted.txt
Category1 Category2 Category3
type1 + + +
type2 - + -
type3 + + -
$ tac table.txt >table-reverse.txt
$ awk -f table2matrix.awk table-reverse.txt
Category1 Category2 Category3
type1 + + +
type2 - + -
type3 + + -
$ grep '+' table.txt >table-incompl.txt
$ awk -f table2matrix.awk table-incompl.txt
Category1 Category2 Category3
type1 + + +
type2 +
type3 + +
$
table.txt
是分開的(從Web瀏覽器複製/粘貼)的空間,table-tabs.txt
是table.txt
與製表符代替空格序列。
從腳本(但不是來自Web瀏覽器中的代碼示例)中可以看出,輸出是製表符分隔的。
在測試了原始示例輸入的一些變體之後,我修復了我的awk腳本。它變得更短一點,更類似於karafka的其他解決方案...
關於輸入數據:列是否由製表符分隔或用空格分隔?它應該如何與輸出有關? – Scheff
@Scheff一切都是製表符分隔 – Jacob
啊哈。我會很快發送一個解決方案。 (它目前用於輸入分隔的空間和用於輸出分隔的選項卡。) – Scheff