Reputation: 153
I have a file, let's call it test.txt, of the following format:
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.
Upvotes: 0
Views: 335
Reputation: 289525
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; otherwise, print a new line.
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|
Upvotes: 3