在条件下加入2个连续的行 [英] join 2 consecutive rows under condition
本文介绍了在条件下加入2个连续的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
typeA;pointA1
typeA;pointA2
typeA;pointA3
typeB;pointB1
typeB;pointB2
结果输出将是:
typeA;pointA1;typeA;pointA2
typeA;pointA2;typeA;pointA3
typeB;pointB1;typeB;pointB2
是否可以为此目的使用sed或awk?
Is it possible to use sed or awk for this purpose?
推荐答案
使用awk可以轻松实现:
This is easy with awk:
awk -F';' '$1 == prevType { printf("%s;%s;%s\n", $1, prevPoint, $0) } { prevType = $1; prevPoint = $2 }'
我假设记录之间的空白行不属于输入;如果是这样,只需在awk之前通过grep -v '^$'
运行输入即可.
I've assumed that the blank lines between the records are not part of the input; if they are, just run the input through grep -v '^$'
before awk.
这篇关于在条件下加入2个连续的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文