Ubuntu – How to edit a range of text between 2 symbols? awk, sed, regex

awkbashregexsed

Using the "*" symbol, (doesn't have to be that one, any special character will do in order to indicate), how can I edit the text from this:

*berry
straw
rasp
blue
boysen
*
blahblah
blahblah
blahblah
*berry
straw
blue
*
blah
*table
vege
pingpong
*

To this:

strawberry
raspberry
blueberry
boysenberry
blahblah
blahblah
blahblah
strawberry
blueberry
blah
vegetable
pingpongtable

Every character after the first matching asterisk will be placed on every line until the 2nd asterisk match is found.

Any leads on how I can go about this? (sed or awk would be preferred, but if you can think of another way, please shoot me your code!)

I know how to remove all lines containing an asterisk, it's just the character placement part I can't think of

Best Answer

This awk code could be enough:

awk -F'*' 'NF == 2 {label = $2; next} {$0 = $0 label} 1'

To break it down:

  • Use * as the field separator. This way, we can simply examine the number of fields (NF) to determine if the beginning or end of a block is reached.
  • When there are two fields, we save the second field in label and continue to the next line.
  • From then, we append that label to the current line, and then print. If the label is empty, we are outside a block and there's no effect. If not, we get the required output.