
我有超過 5000 個空格分隔的行,如下所示:
Item_A: Acou#1 Bla#5
Item_B: Acou#1 Elfa#2 Flq#2
Item_C: Acou#1 Bla#4 Elfa#2 Flq#2
Item_D: Agly#3 Bla#4 Elfa#2
我想製作一個表格,其中包含表格中所有內容和數量的通用標題,如下所示,
Acou Agly Bla Elfa Flq
Item_A: 1 0 5 0 0
Item_B: 1 0 0 2 2
Item_C: 1 0 4 2 2
Item_D: 0 3 4 2 0
我曾經 grep 包含“Acou”的行,然後是“Bla”等。然後在 Excel 中進行編輯以量化它們並將所有單獨的文件連接到一個文件。然而,這花了很多時間。
答案1
BEGIN { OFS = "\t" }
# Collect headers from data
FNR == NR {
for (i = 2; i <= NF; ++i)
if (!($i in heads))
heads[$i]
next
}
# Output header
FNR == 1 {
line = "Items"
for (j in heads)
line = line OFS j
print line
}
{
line = $1
# Iterate through the header items, testing each field against it
for (j in heads) {
found = 0 # assume not found
for (i = 2; !found && i <= NF; ++i)
if ($i == j)
found = 1 # matches header
line = line OFS found
}
print line
}
在您的資料上執行此命令(刪除空白行後):
$ awk -f script.awk file file
Items Acou#1 Bla#4 Bla#5 Elfa#2 Agly#3 Flq#2
Item_A: 1 0 1 0 0 0
Item_B: 1 0 0 1 0 1
Item_C: 1 1 0 1 0 1
Item_D: 0 1 0 1 1 0
請注意,您必須指定輸入資料檔案兩次。這是因為我們掃描了兩次。在第一次掃描中,我們收集每行(FNR == NR
區塊)上的資料項目。在第二次掃描中,我們根據每行資料測試每個收集的資料項目(標題)。
輸出只是0
標頭中的欄位是否存在於該行的資料中,以及1
是否存在。這不是相當你要求什麼,所以...
截斷 處的標題#
並使用 後的部分#
作為要顯示的資料的變體:
BEGIN { OFS = "\t" }
# Collect headers from data
FNR == NR {
for (i = 2; i <= NF; ++i) {
split($i, h, "#")
if (!(h[1] in heads))
heads[h[1]]
}
next
}
# Output header
FNR == 1 {
line = "Items"
for (j in heads)
line = line OFS j
print line
}
{
line = $1
# Iterate through the header items, testing each field against it
for (j in heads) {
found = 0 # assume not found
for (i = 2; !found && i <= NF; ++i) {
split($i, h, "#")
if (h[1] == j)
found = h[2] # matches header
}
line = line OFS found
}
print line
}
運行它:
$ awk -f script.awk file file
Items Elfa Bla Acou Agly Flq
Item_A: 0 5 1 0 0
Item_B: 2 0 1 0 2
Item_C: 2 4 1 0 2
Item_D: 2 4 0 3 0
請注意,列的順序不一定是排序的(因為它們作為鍵儲存在關聯數組中)。我將其作為練習留給讀者對它們進行排序。
答案2
如果你不介意丟GNU 資料混合到混合中,然後您可以簡單地序列化條目,然後對它們進行交叉製表:
awk '
{for (i=2;i<=NF;i++) {split($i,a,"#"); print $1,a[1],a[2]}}' OFS='\t' file |
datamash --filler=0 crosstab 1,2 count 3
Acou Agly Bla Elfa Flq
Item_A: 1 0 1 0 0
Item_B: 1 0 0 1 1
Item_C: 1 0 1 1 1
Item_D: 0 1 1 1 0
或者,使用 GNU awk(允許多維數組):
gawk '
BEGIN {
OFS="\t";
PROCINFO["sorted_in"] = "@ind_str_asc";
}
{
for (i=2;i<=NF;i++) {
split($i,a,"#");
h[a[1]] = 1;
t[$1][a[1]] += a[2];
}
}
END {
for (j in h) printf("\t%s", j);
printf "\n";
for (i in t) {
printf("%s",i);
for (j in h)
printf("\t%d", j in t[i] ? t[i][j] : 0);
printf "\n";
}
}' file
Acou Agly Bla Elfa Flq
Item_A: 1 0 5 0 0
Item_B: 1 0 0 2 2
Item_C: 1 0 4 2 2
Item_D: 0 3 4 2 0