有条件的awk结合多种线 [英] Awk combining multiple lines conditionally

查看:123
本文介绍了有条件的awk结合多种线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,如果他们匹配的ID来从价值观不同长度的多行合并成一条线。

I want to combine values from multiple lines of varying length into one line if they match IDs.

输入的例子是:

ID:  Value:
a-1  49
a-2  75
b-1  120
b-2  150
b-3  211
c-1  289
d-1  301
d-2  322

所需的输出的例子是:

Desired output example is:

ID:  Value:
a 49,75
b 120,150,211
c 289
d 301,322

我怎么会写一个awk前pression(或用sed或者grep的或东西),以检查是否匹配的ID,然后上打印所有这些值来一条线?我可以只打印过程
他们分为不同的栏目,后来将它们结合起来,所以真的问题只是有条件地打印,如果ID匹配,如果不启动一个新行。

How would I write an awk expression (or sed or grep or something) to check if the IDs matched, and then to print all those values on to one line? I can of course just print them into different columns and combine them later, so really the problem is just conditionally printing if the IDs match and if not starting a new line.

推荐答案

在awk中,如果您的ID被聚集在一起:

In awk, if your IDs are clustered together:

awk 'NR==1 {print $0}
NR > 1 {sub("-.*", "", $1)}
NR == 2 {prev=$1; printf "%s %s", $1, $2}
NR > 2 && prev == $1 {printf ",%s", $2}
NR > 2 && prev != $1 {prev=$1; printf "\n%s %s", $1, $2}' your_input_file

这篇关于有条件的awk结合多种线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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