Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file with some data in this form:

Prefix text: First Name, Second Name, Third--

The prefix differs by line. The number of names varies from one to several. The suffix (-- in the example) is optional and non-alphabetic. I need to expand the comma-separated list of names into multiple lines (easy: s/,/\n/g), but in such a way that prefix and suffix (if present) surround each of the new entries:

Prefix text: First name--
Prefix text: Second name--
Prefix text: Third--

Instead of banging out a too-long python script, I thought it'd be more fun to ask if someone here can think of the perfect one-liner. Any ideas?

share|improve this question
Drive-by downvoter: If you see something wrong with the question, please clue us in. – alexis Jan 22 '14 at 14:22
up vote 3 down vote accepted
perl -lne 'if(/^(.*?: )(.*?)(\W*)$/){print"$1$_$3"for split/, /,$2}'
share|improve this answer
Beautiful! Works like a charm, thanks! Right, print X for split. – alexis Jan 22 '14 at 0:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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