How to get matching string and correponding first line of a paragraphi from a text file . File contains multiple paragraph

Viewed 36

I am having below data in a text file

[abcd]
sgsgdh
shshdhd
shdhdhd

[defg]
sgshdhd
shshdjd
shshdhd
shehddkd
shejdjd

[jkhl]
sgshdjdj
shejdjr
shshdhd
sgehdjd
shehdjjd

output

[abcd] : shshdhd

[defg] : shshdhd

[defg] : shshdhd

and so on

1 Answers
awk 'BEGIN {RS = ""; FS = "\n"; OFS = " : "} {print $1, $2}' file
[abcd] : sgsgdh
[defg] : sgshdhd
[jkhl] : sgshdjdj

If you want the blank lines in the output, add ORS = "\n\n" to the BEGIN block

Related