awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file.

caw-enabled               true
controller-tag            -
initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
operational-status        ok
port-name-enabled-status  [P00000000476043B3-A0-FC01,true,ok, P00000000476046CC-A0-FC00,true,ok,
                          P00000000477043B3-B0-FC00,true,ok, P00000000477046CC-B0-FC01,true,ok]
ports                     [P00000000476043B3-A0-FC01, P00000000476046CC-A0-FC00,
                          P00000000477043B3-B0-FC00, P00000000477046CC-B0-FC01]
caw-enabled               true
controller-tag            -
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a,
                          NETIK0102_UCS_HBA3_b]
operational-status        ok
port-name-enabled-status  [P00000000476043B8-A0-FC02,true,ok, P0000000047604458-A0-FC03,true,ok,
                          P00000000477043B8-B0-FC03,true,ok, P0000000047704458-B0-FC02,true,ok]
ports                     [P00000000476043B8-A0-FC02, P0000000047604458-A0-FC03,
                          P00000000477043B8-B0-FC03, P0000000047704458-B0-FC02]

How do I print the initiator info when it is matched in the 2nd instance above? I would like the ouput to be like this.

 
initiators [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a, NETIK0102_UCS_HBA3_b]

---------- Post updated at 11:43 AM ---------- Previous update was at 11:04 AM ----------

I am able to extract the info I need but I want it to print on the same line. I am using

cat file | awk '/initiators/,/operational-status/ {print $0}' | grep -v operational-status

And this gets me the output in this format.

initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a,
                          NETIK0102_UCS_HBA3_b]

But I want it to output in the following format

initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a,NETIK0102_UCS_HBA3_b]

---------- Post updated at 12:02 PM ---------- Previous update was at 11:43 AM ----------

Seems like a roudabout way to do it but I got it working by using the following command. Alternatives are welcome.

cat file | awk '/initiators/,/operational-status/ {print $0}' | grep -v operational-status | sed 's/ //g' | sed 's/initiators/initiators    /g' | nawk '/,$/ {printf "%s",substr($0, 1, length-1);next} 1'

Try this one:

cat file | tr '\n' '$' | sed 's/$                          / /g' | tr '$' '\n' | awk '$1 == "initiators" {print}'

Note that this assumes that "$" does not appear in your text. If it does, find some character that isn't in your text and replace all $'s in that line with that new character.

Try also

awk '/initi/ {if (!sub(/]$/,"&")) {getline X; gsub(/^ +/," ", X); $0=$0 X}; print}' file
initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a, NETIK0102_UCS_HBA3_b]
1 Like

Thanks. That works great!