[英]transposing multiple columns in bash
像這樣的半成品文件
Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764
我希望將它們轉換為
Input1 file1:45764, file1:878755, file1: 899787
Input2 file1:45676, file1:769678, file1: 6454764
有猜到嗎? 提前致謝
awk '{b[$1]=b[$1] $2$3" "}END{for (i in b) print i,b[i]}' inputFile
將產生輸出為
Input1 file1:45764 file1:878755 file1:899787
Input2 file1:45676 file1:769678 file1:6454764
它能做什么?
{b[$1]=b[$1] $2$3" "}
創建一個數組b
追加第二和第三列(因為示例中file
和value
之間存在一些空格)。 $2$3
放入陣列。 該數組是一個由Inputx
索引的關聯數組,其中x
為1,2...
那是
b['Input1'] = 'file1:45764 file1:878755 file1:899787'
END
塊在輸入文件input
末尾執行,
for (i in b) print i,b[i]}
打印b
數組的內容
如果要在輸出中使用逗號,請嘗試
awk '{if(b[$1])b[$1] = b[$1]", "; b[$1] = b[$1] $2 $3}; END{for(i in b)print i, b[i]}'
或稍微更簡短
awk '{b[$1]=b[$1](b[$1]?", ":"")$2$3}END{for(i in b)print i,b[i]}'
產量
Input1 file1:45764, file1:878755, file1:899787
Input2 file1:45676, file1:769678, file1:6454764
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.