如何基于AWK/UNIX中的通用字段将行中的分隔字段合并为一个字段 [英] How to merge separated fields in rows into one based on common fields in AWK / UNIX

查看:89
本文介绍了如何基于AWK/UNIX中的通用字段将行中的分隔字段合并为一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然只是UNIX的新用户,尤其是AWK的新用户.我在合并基于前2列值的行时遇到问题.我来自文件的原始数据如下:

I am still just a new user to UNIX and especially to AWK. I am having the problem to merge rows based on first 2 columns values. My original data from a file as below:

Original data content
========================

ID1 ID2 Field1  Field2
1   1   11F1    11F2
1   2   12F1    12F2
2   1   21F1    21F2
2   2   22F1    22F2
ID1 ID2 Field3  Field4
1   1   11F3    11F4
1   2   12F3    12F4
2   1   21F3    21F4
2   2   22F3    22F4
ID1 ID2 Field5  Field6
1   1   11F5    11F6
1   2   12F5    12F6
2   1   21F5    21F6
2   2   22F5    22F6

您注意到,列被分为不同的行/块,但ID字段和列标题仍然可用并重复.因此,我想要实现的目标如下:

As you noticed, columns are split into different rows/block but IDs fields and columns heading are still available and repeated. So what i want to achieve is as follow:

ID1 ID2 Field1  Field2  Field3  Field4  Field5  Field6
1   1   11F1    11F2    11F3    11F4    11F5    11F6
1   2   12F1    12F2    12F3    12F4    12F5    12F6
2   1   21F1    21F2    21F3    21F4    21F5    21F6
2   2   22F1    22F2    22F3    22F4    22F5    22F6

将所有内容合并为一个块/表. 但是不知道如何在AWK中完成此操作,或者用AWK是否有可能实现.

Merge all into as a single block/table. But don't know how to do it in AWK, or is it possible do achieve with AWK.

非常感谢. 高浩

推荐答案

是的,可以使用awk:

awk ' 
{ key = $1 FS $2 }
!seen[key]++ { keys[++total] = key }
{ values[key] = ( key in values ? values[key] FS $3 FS $4 : $3 FS $4 ) }
END {
    for (cnt=1; cnt<=total; cnt++) 
    print keys[cnt], values[keys[cnt]]
}' file

  • 将第一列和第二列作为键
  • 使用数组seen记住出现的顺序.
  • 测试您的键是否存在于数组中(我们在这里使用三元运算).如果存在,则将现有值附加新的数据集.如果不存在,则将其作为值推送.
  • END块中,迭代并打印.
  • 如果您有很多列,则将该列存储在变量中,并在存储之前从其中删除键.
    • Form the first and second column as key
    • Using an array seen remember the order of occurrence.
    • Test if your key is present in an array (we are using ternary op here). If it is present, append the existing value with new data set. If it is not present, push it as the value.
    • In the END block, iterate and print.
    • If you have many columns then store the column in a variable and remove the keys from it before storing.
    • 这篇关于如何基于AWK/UNIX中的通用字段将行中的分隔字段合并为一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆