3

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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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