[英]Awk asking combine two files
我已經通過AWK命令將兩個不同的文件與Same Key結合在一起。 如果沒有鍵匹配,則將File1和File2進行比較,然后只需輸入“ \\ t \\ t \\ t”即可。
我有下面的AWK命令。
awk -F"\t" '
{key = $1}
NR == 1 {header = key}
!(key in result) {result[key] = $0 ; next}
{ for (i=2; i <= NF; i++) result[key] = result[key] FS $i }
END {
print result[header],"\tValue2","\tValue3","\tValue4"
delete result[header]
PROCINFO["sorted_in"] = "@ind_str_asc" # if using GNU awk
for (key in result) print result[key]
}
' $1 $2 > $3
示例組合File1
Key Value1
A 10000
B 20000
C 30000
D 40000
文件2
B 50000 20000 10000
C 20000 10000 50000
然后預期結果
Key Value1 Value2 Value3 Value4
A 10000 - - -
B 20000 50000 20000 10000
C 30000 20000 10000 50000
D 40000 - - -
我的AWK命令顯示
Key Value1 Value2 Value3 Value4
A 10000
B 20000 50000 20000 10000
C 30000 20000 10000 50000
D 40000
我已經嘗試過以下幾種方法
!(key in result) {result[key] = $0"\t-\t-\t-" ; next}
但這似乎並不涵蓋所有情況。 有人有更好的主意嗎? 謝謝!
此解決方案不會硬編碼File2中有3個額外字段
awk '
BEGIN { FS = OVS = "\t" }
NR == FNR {
key = $1
$1 = ""
store[key] = $0
num_extra_fields = NF-1
next
}
FNR == 1 {
printf "%s", $0
for (i=1; i <= num_extra_fields; i++)
printf "%sValue%d", OFS, i+(NF-1)
print ""
next
}
$1 in store {
print $0 store[key]
next
}
{
for (i=1; i <= num_extra_fields; i++)
$(++NF)="-"
print
}
' file2 file1
由於stackoverflow如何顯示選項卡,因此輸出看起來有些奇怪
Key Value1 Value2 Value3 Value4
A 10000 - - -
B 20000 20000 10000 50000
C 30000 20000 10000 50000
D 40000 - - -
要修復代碼,您需要跟蹤file2中更新結果的鍵。 更改
{ for (i=2; i <= NF; i++) result[key] = result[key] FS $i }
至
{ updated[key]=1; for (i=2; i <= NF; i++) result[key] = result[key] FS $i }
然后在END塊中進行更改
for (key in result) print result[key]
至
for (key in result) {
if (!(key in updated)) result[key] = result[key] FS "-" FS "-" FS "-"
print result[key]
}
使用awk,您可以執行以下操作:
awk -v OFS='\t' 'NR==FNR {if (!n) {n=NF-1; s="-"; for(i=2; i<=n; i++) s=s OFS "-"}
a[$1]=$0;next} $1 in a{sub($1, "&" OFS $2, a[$1]);
print a[$1]; next} {print $0, s}' file2 file1
A 10000 - - -
B 20000 50000 20000 10000
C 30000 20000 10000 50000
D 40000 - - -
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.