鉴于此输入和comm
输出:
$ cat file1
YAL002W
YAL003W
YAL008W
$ cat file2
YAL004W
YAL005C
YAL008W
YAL011W
$ comm file1 file2
YAL002W
YAL003W
YAL004W
YAL005C
YAL008W
YAL011W
这将满足您的要求:
$ cat tst.awk
BEGIN { FS=OFS="\t" }
{
colNr = NF
rowNr = ++rowNrs[colNr]
val[rowNr,colNr] = $NF
numCols = (colNr > numCols ? colNr : numCols)
numRows = (rowNr > numRows ? rowNr : numRows)
}
END {
for (rowNr=1; rowNr<=numRows; rowNr++) {
for (colNr=1; colNr<=numCols; colNr++) {
printf "%s%s", val[rowNr,colNr], (colNr<numCols ? OFS : ORS)
}
}
}
.
$ comm file1 file2 | awk -f tst.awk
YAL002W YAL004W YAL008W
YAL003W YAL005C
YAL011W
但是当然,您可以直接跳过对comm
awk 的调用并立即使用:
$ cat tst.awk
BEGIN { FS=OFS="\t" }
NR==FNR {
file1[$0]
next
}
{
if ($0 in file1) {
colNr = 3
delete file1[$0]
}
else {
colNr = 2
}
rowNr = ++rowNrs[colNr]
val[rowNr,colNr] = $0
}
END {
for (v in file1) {
colNr = 1
rowNr = ++rowNrs[colNr]
val[rowNr,colNr] = v
}
numRows = (rowNrs[1] > rowNrs[2] ? rowNrs[1] : rowNrs[2])
numRows = (numRows > rowNrs[3] ? numRows : rowNrs[3])
numCols = 3
for (rowNr=1; rowNr<=numRows; rowNr++) {
for (colNr=1; colNr<=numCols; colNr++) {
printf "%s%s", val[rowNr,colNr], (colNr<numCols ? OFS : ORS)
}
}
}
.
$ awk -f tst.awk file1 file2
YAL002W YAL004W YAL008W
YAL003W YAL005C
YAL011W