Match a pattern, insert the pattern, until next match

Viewed 329

I'm struggling to format a file according to the desired output, using bash tools. Here's a sample:

address="192.168.1.1"
portid="443"
portid="2000"
address="192.168.1.2"
portid="443"
portid="2000"

In essence, what I am trying to achieve is to search for a pattern (in this case, the whole IP address line), and prepend it to each subsequent line until the next match (before the next IP address). The desired output is this:

address="192.168.1.1"portid="443"
address="192.168.1.1"portid="2000"
address="192.168.1.2"portid="443"
address="192.168.1.2"portid="2000"

How can I achieve this using grep, awk or sed?

4 Answers
Related