使用 AWK 合并基于第一列的唯一行 [英] Using AWK to merge unique rows based on column one
本文介绍了使用 AWK 合并基于第一列的唯一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个 AWK 脚本来汇总大型文本文件中的数据.结果数据的顺序很重要,所以我不能使用排序.
我尝试了 FNR==NR 的不同变体,但没有任何运气
输入文件
高度 3.5重量 12.323岁::高度 4.5重量 15.5年龄 31::
预期产出
高度 3.5 4.5重量 12.3 15.5年龄 23 31
解决方案
使用 awk:
awk '{a[$1]=a[$1] FS $2} END{for(i in a) print i a[i]}' 文件
输出:
<前>重量 12.3 15.5高度 3.5 4.5:年龄 23 31<小时>参见:8 个强大的 Awk 内置变量——FS、OFS、RS、ORS、NR、NF、FILENAME、FNR
I am trying to write an AWK script to summarize data on a large text file. The order of the resulting data is important so i can't use sort.
I have tried different variations of FNR==NR but haven't had any luck
Input file
Height 3.5
Weight 12.3
Age 23
:
:
Height 4.5
Weight 15.5
Age 31
:
:
Expected Output
Height 3.5 4.5
Weight 12.3 15.5
Age 23 31
解决方案
With awk:
awk '{a[$1]=a[$1] FS $2} END{for(i in a) print i a[i]}' file
Output:
Weight 12.3 15.5 Height 3.5 4.5 : Age 23 31
Derived from: how to merge rows that share unique IDs into a comma separated table
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
这篇关于使用 AWK 合并基于第一列的唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文