请您尝试以下操作。这也应该适用于 3 个以上的 Input_file(尽管仅使用 OP 的示例 3 Input_file 进行了测试,但应该可以使用)。
awk '
FNR==1{
count++
}
{
a[$1]
b[count,$1]=$2
}
END{
for(j in a){
for(i=1;i<=count;i++){
printf("%s%s%s",i==1?j OFS:"",b[i,j]?b[i,j]:" ----- ",i==count?ORS:OFS)
}
}
}
' Input_file1 Input_file2 Input_file3 | column -t
输出如下。
207.46.13.90 37556 62343 85343
40.77.167.109 21824 21824 -----
157.55.39.51 34268 58451 59876
157.55.39.253 19683 ----- 19683
157.55.39.200 ----- 37675 37675
说明:在此添加对上述代码的详细说明。
awk ' ##Starting awk program from here.
FNR==1{ ##Checking condition if this is first line then do following.
count++ ##Creating a variable count whose value is increasing each time FNR==1 for each Input_file first line.
}
{
a[$1] ##Creating an array named a whose index is $1 first field of current line for Input_file(s).
b[count,$1]=$2 ##Creating an array named b whose index is count,$1 and value is $2 of current line.
}
END{ ##Starting END BLOCK for this awk program here.
for(j in a){ ##Looping through array a all elements from here.
for(i=1;i<=count;i++){ ##Running a for loop from i=1 till value of count.
printf("%s%s%s",i==1?j OFS:"",b[i,j]?b[i,j]:" ----- ",i==count?ORS:OFS) ##Using printf statement where first condition its checking is i==1 TRUE then print j OFS OR print NULL, checking condition if element b[i,j] is NOT NULL then print its value else print NULL. Final condition is if i==count then print new line else print space.
} ##Closing BLOCK for, for loop of i=1 to i<=count.
} ##Closing BLLOCK for, for loop (j in a) here.
} ##Closing BLOCK for END BLOCK of this awk program.
' Input_file1 Input_file2 Input_file3 | column -t ##Mentioning Input_file names here and sending its output to column -t command to get correct spaces in output.