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
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 patternA 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, whileendpat
controls where the pattern ends.
/^end/? ORS:","
- set delimiter for the current item within a rangeIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With