I have this lines:
alpha: beta
beta: alpha, beta
omega: beta, gamma, alpha
gamma:
alpha
beta
gamma
epsilon: alpha
I want to join line contains
This also works if the input is provided in a file as an argument to the script, and is a little shorter:
$/=$.;$_=<>;s/\s?\n/, /g;s/, (\w+:),?/\n$1/g;s/, $/\n/;print
It's not very readable, but it works with strict
and warnings
.
Output:
alpha: beta
beta: alpha, beta
omega: beta, gamma, alpha
gamma: alpha, beta, gamma
epsilon: alpha
It can probably be shortened even further. -ape
or #!perl -ap
would be a good start.