I want to add a new line after each name.  My file contains:

Smoker, M., Schauser, L., Yoshioka, H.,

what I want:

Smoker, M
Schauser, L
Yoshioka, H

so I used multiple options in sed:

sed 's%\., %\n%g'
sed 's%\., %\r%g'
sed -E 's%\., %\r\n%g'

and similar but I always get an r or n instead of a newline; e.g.,

Smoker, Mn Schauser, Ln Yoshioka, Hn
         ↑            ↑            ↑

I am using Mac OS sed.

  • Welcome to Unix & Linux Stack Exchange. It would appear that you have accidentally created two accounts. You should use the contact form and select “I need to merge user profiles” to have your accounts merged. In order to merge them, you will need to provide links to the two accounts. For your information, these are unix.stackexchange.com/users/216640/aitana and unix.stackexchange.com/users/216641/aitana. You’ll then be able to edit, comment on, and accept answers to this question. – Scott Feb 17 '17 at 21:22
  • (1) You know that people usually use / as the delimiter in their s commands, right?  The main exception is if you are searching for or replacing with a literal / character.  Why are you using %? (2a) Do you want to remove a . (period) after the initial from each name?  If so, you should say so. (2b) Does every name in your file end with an initial followed by a period? (3) There are lots of other details that would help us write a really good answer. For example, do you always want to split lines at every , (comma)? … (Cont’d) – G-Man Feb 18 '17 at 2:11
  • (Cont’d) ... What if there’s a comma without a space after it?  What if there are multiple spaces?  Will there always be a comma after the last name?  Will there ever be space(s) after the comma at the end of the line?  Is your file just the one line that you showed us, or are there multiple such lines?  Do you want (or not want, or not care) a blank line inserted into the output for each input line?  Can your input ever contain line(s) that don’t contain name(s) — i.e., that don’t contain comma(s) — or that contain only one name? If so, what do you want done with them? – G-Man Feb 18 '17 at 2:11

When your "sed" does not support "\n" on the RHS of s/// you can try the following approach:

sed -e '/\.,/!d' -e 'G;s/\.,[ ]*\(.*\(.\)\)$/\2\1/' -e 'P;D' < yourfile

Assuming there are no TABs and the input data as shown. We rely on our native "sed" to supply the newline by means of the 'G' to be injected into the pattern space, and where from it can be moved around anonymously as desired to get the result we want.

  • (1) Very clever!  It works even in POSIX sed! (2) Is there any particular reason why you use [ ]* when ⁠ * would work? (3) Why do you mention TAB? – G-Man Feb 18 '17 at 2:12
  • The [ ]* makes it stand out as opposed to just *. TABs are also valid whitespace and this will fail if the input had TAB-separated data or even a mixture of TABs & spaces. – Rakesh Sharma Feb 18 '17 at 2:52

This appears to work using BSD sed and bash:

sed $'s/\., */\\\n/g'

It asks to replace every sequence of a literal dot, comma, and any number of spaces to a newline.

If you do not have a shell that supports the $'\n' syntax for newlines, you can try the following:

sed 's/\., */\

I.e., type everything until the backslash, then hit Enter and complete with /g'. Alternatively, depending on how your shell input mode is set, Ctrl+V followed by Ctrl+J may insert a newline character without evaluating the command.

  • Note, all of those will print a trailing newline; use, for example, grep . to get rid of it if needed. – dhag Feb 17 '17 at 20:45

You definitely want \n and not \r .


sed 's%\., %\n%g'

worked for me, so I don't know why you're having trouble.

Maybe a double backslash will solve your problem. Or instead of \n , try control-v control-m (this will look like ^M on your screen).

  • Works with GNU sed, yes, but the default sed on macOS can't insert a newline like that. – Kusalananda Feb 17 '17 at 18:37

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.