Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk concatenate strings till contain substring

Tags:

regex

bash

awk

I have a awk script from this example:

awk '/START/{if (x) print x; x="";}{x=(!x)?$0:x","$0;}END{print x;}' file

Here's a sample file with lines:

$ cat file
START
1
2
3
4
5
end
6
7
START
1
2
3
end
5
6
7

So I need to stop concatenating when destination string would contain end word, so the desired output is:

START,1,2,3,4,5,end
START,1,2,3,end
like image 680
cardinal-gray Avatar asked Dec 13 '17 15:12

cardinal-gray


1 Answers

Short Awk solution (though it will check for /end/ pattern twice):

awk '/START/,/end/{ printf "%s%s",$0,(/^end/? ORS:",") }' file

The output:

START,1,2,3,4,5,end
START,1,2,3,end

  • /START/,/end/ - range pattern

A range pattern is made of two patterns separated by a comma, in the form ‘begpat, endpat’. It is used to match ranges of consecutive input records. The first pattern, begpat, controls where the range begins, while endpat controls where the pattern ends.

  • /^end/? ORS:"," - set delimiter for the current item within a range
like image 64
RomanPerekhrest Avatar answered Sep 21 '22 12:09

RomanPerekhrest