2

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?

1
  • On macOS Ventura and Sonoma this works without a problem (just checked it on Sonoma 14.4.1). I also prefer to split by a comma along with a whitespace , to avoid carrying the whitespace to a new line in the resulting list: defaults domains | sed 's/, /\n/g'. Commented Apr 5 at 1:40

2 Answers 2

0

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/,/\
/g'

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.

1
  • 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. Commented Jul 31, 2015 at 18:39
2

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.

8
  • 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
    Commented Jul 31, 2015 at 18:11
  • LOL! Never gotten a negative before. I thought it would be prudent to answer my own question, but maybe not? Commented Jul 31, 2015 at 18:38
  • I believe you were voted down because you didn't edit your original post with your solution.
    – fd0
    Commented Jul 31, 2015 at 18:56
  • 1
    @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? Commented Jul 31, 2015 at 19:03
  • @ user3439894 I did not down vote Chris. I do not appreciate your assumption,
    – fd0
    Commented Jul 31, 2015 at 19:09

You must log in to answer this question.

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