gpt4 book ai didi

linux - 如果第 2 列等于某个值,如何连接每 5 行?

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:13 25 4
gpt4 key购买 nike

我有一个文件,如果第二列的数字为 2,我想连接接下来的 5 行,例如:

67 2
a b c
a b
0.1 0.2 0.3 0.4
0.3 0.9 0.7 0.1
09 3
b v c
5 6 7 8
78 2
p o p
q d
1.0 0.9 0.8 0.7
0.4 0.3 0.2 0.1

输出应该是:

67 2 a b c a b 0.1 0.2 0.3 0.4 0.3 0.9 0.7 0.1
78 2 p o p q d 1.0 0.9 0.8 0.7 0.4 0.3 0.2 0.1

最佳答案

awk解决方案:在每遇到第二列中带有 2 的行时连接 5 行(包括 pattern 行)(连接的行除外):

awk '$2==2{i=4;tail=$0; while (i-- && (getline nl) > 0) { tail=tail FS nl } print tail}' file

输出:

67 2 a b c a b 1 2 3 4 0 9 7 1
78 2 p o p q d 0 9 8 7 4 3 2 1

关于linux - 如果第 2 列等于某个值,如何连接每 5 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44212364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com