0

I've got a file with a structure like this:

M: a
M: b
Player: c
M: d
Sequencer.Play( SpineAnimation(foo, speaker)@0 )
Player: e
M: f
Sequencer.Play( SpineAnimation(bar, speaker)@0 )
...

How do I convert this into a file like this:

M: a
M: b
Player: c
M foo: d
Player: e
M bar: f
...

In other words, if the next line has Sequencer.Play( SpineAnimation(..., I want to take the word after it and put it before the colon on the current line.

Not sure what the best tool for this would be, I'm thinking awk, but then again, I think it'll be pretty hairy with that tool. I'm open to use any command to accomplish this. I tried to do something like the answer here: Merge two lines into one picking selected words from both But this question seems non-trivially different because the Sequencer.Play line isn't always there.

1

You could try something like this, using sed:

$ sed -e 'N;s/M:\(.*\)\nSequencer.Play( SpineAnimation(\([^,]*\).*/M \2:\1/' -e 'P;D' file
M: a
M: b
Player: c
M foo: d
Player: e
M bar: f

The N ... P;D commands maintain a 2-line buffer in pattern space, then the s command matches the 2-line pattern, capturing the parts between the M and the newline, and from the SpineAnimation( and the following , and re-substituting them after the M

| improve this answer | |
  • Well, a Sequencer.Play( SpineAnimation(bar, speaker)@0 ) line could be after a Player: line, too, so it doesn't work for that situation. I think I can solve that with another group, though – Daniel Kaplan Oct 29 '19 at 2:24
  • This worked for me: sed -e 'N;s/\(.*\):\(.*\)\nSequencer.Play( SpineAnimation(\([^,]*\).*/\1 \3:\2/' -e 'P;D' file – Daniel Kaplan Oct 29 '19 at 2:27
  • @DanielKaplan ... sounds good, I was about to suggest exactly that – steeldriver Oct 29 '19 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.