简体   繁体   English

使用awk有条件地加入行

[英]Using awk to conditionally join lines

I have a file, let's call it test.txt, of the following format: 我有一个文件,我们称其为test.txt,格式如下:

1|2|3|4|
1|2|3|4|
1|2|
3|4|
1|2|3|4|
1|2|3|4|

You will see that the third line has been split over 2 lines and I need to be able to process the file such that all such occurrences are re-joined to result in: 您将看到第三行已分成两行,我需要能够处理该文件,以便重新结合所有出现的结果,从而导致:

1|2|3|4|
1|2|3|4|
1|2|3|4|
1|2|3|4|
1|2|3|4|

I have been trying to achieve this by first grabbing the number of pipes on each line: 我一直在尝试通过首先获取每行上的管道数量来实现这一点:

cat test.txt | awk -F'|' '{print NF -1}

What I want to be able to do is to extend this such that any lines that do no match the specified number of pipes, in this example four, are joined on to the next line until they do. 我想要做的就是扩展它,以便将与指定数量的管道不匹配的任何行(在此示例中为四个)连接到下一行,直到它们匹配为止。

Can anyone point me in the right direction please? 谁能指出我正确的方向? Thanks. 谢谢。

You can play with the number of fields a little bit: 您可以玩一些字段:

awk -v FS="|" '{printf "%s%s", $0, (f+NF<5?"":RS); f+=NF} f>=5 {f=0}' file

This sets the field separator to | 这将字段分隔符设置为| and from them, keeps counting how many fields have been printed so far. 并从中继续计算到目前为止已打印了多少个字段。 If the number is lower than 5, keep printing in the same line; 如果数字小于5,请保持在同一行中打印;否则,请保留该行。 otherwise, print a new line. 否则,请打印新行。

Test 测试

For an input file like 对于像这样的输入文件

$ cat a
1|2|3|4|
1|2|3|4|
1|2|
3|4|
7|2|3|4|
1|2|3|4|
1|
2|
3|4|

See the output: 查看输出:

$ awk -v FS="|" '{printf "%s%s", $0, (f+NF<5?"":RS); f+=NF} f>=5 {f=0}' a
1|2|3|4|
1|2|3|4|
1|2|3|4|
7|2|3|4|
1|2|3|4|
1|2|3|4|

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM