Just a quick question here. I'm trying to format the output of the command:

defaults domains

so that it puts each domain on a new line, so I can use that parsed output in a different command.

I've tried:

defaults domains | sed 's/,/\n/g'

Which should take the output of default, find each comma, and replace with a new line correct? Instead it just removes the comma and keeps each item on the same line.

I thought, maybe it's something with the encoding of the dafaults command output, so I dumped to a file and tried parsing that, but no dice.

Anyone have any idea how I can achieve this?


In OS X using the default BSD sed you can do it a few different ways, here's a couple ways.

defaults domains | sed 's/,/\

You'd press Enter after typing the backslash, no "n" and finish off with the /g' on the next line. This substitutes a literal newline.

This also works:

defaults domains | sed 's/,/\'$'\n/g'

Some of us install the GNU version of various command line programs such as this and your original defaults domains | sed 's/,/\n/g' would work just fine with GNU sed.

That all said, I have to say that since tr could do what you needed/wanted so easily it was the way to go.

  • Yeah, I saw that in BSD varients you actually put a new line instead of the carriage return indicator, but I didn't get around to trying it before I got tr to work. Jul 31 '15 at 18:39

Never mind. Not sure why sed didn't work the way I wanted it to, but I just used:

tr ',' '\n'

and it worked fine.

  • tr will work multiple times faster than using sed, if tr can do the task. Even chaining tr with sed will work faster than just using sed.
    – fd0
    Jul 31 '15 at 18:11
  • LOL! Never gotten a negative before. I thought it would be prudent to answer my own question, but maybe not? Jul 31 '15 at 18:38
  • I believe you were voted down because you didn't edit your original post with your solution.
    – fd0
    Jul 31 '15 at 18:56
  • @fd0, That is no excuse for down-voting and it is perfectly fine for the author of the OP to post as an answer to their own question, even if the solution is different then what was being asked about. In any event I up-voted the question as it's a valid answer even though mine did address the sed issues encountered in the OP. See:Can I answer my own question? Jul 31 '15 at 19:03
  • @ user3439894 I did not down vote Chris. I do not appreciate your assumption,
    – fd0
    Jul 31 '15 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .