答案1
尝试:
awk '
!($3 in T1) {SQ[++CNT] = $3
}
{T1[$3] = T1[$3] DL[$3] $1
T2[$3] = T2[$3] DL[$3] $2
DL[$3] = ","
}
END {for (i=1; i<=CNT; i++) printf "%s\t%s\t%s\t", T1[SQ[i]], T2[SQ[i]], SQ[i]
print _
}
' file
1,3 2,4 5s 5 6 16s 7 8 23s
答案2
GNU 的替代大锤awk
使用换行符或空格作为字段分隔符-F'[\n ]'
,这也需要RS=""
避免任何\n
被视为记录分隔符。
awk -F'[\n ]' 'BEGIN{RS=""; OFS="\t"}
{print substr(FILENAME,1,length(FILENAME)-4), $1","$4, $2","$5, $3, $7, $8, $9, $10, $11, $12}
' *.txt
输出:
File1 1,3 2,4 5s 5 6 16s 7 8 23s