user3261569
user3261569

Reputation: 25

Make one space after every comma?

In Linux, using sed.. how can I make sure that every comma is followed by only one space?

(If there is no space, then add a space after the comma. If there is more than 1 space after the comma, then trim it back so there is only 1.)

I've tried Googling for a solution, but haven't been able to find anything.

Upvotes: 1

Views: 5641

Answers (3)

Tim Zimmermann
Tim Zimmermann

Reputation: 6420

If you want to make sure that there is exactly one space after every comma, you can use:

sed 's/, */, /g' file

Call it as follows to do the substitution directly in the file instead of only printing it:

sed -i 's/,/, /g; s/,\s\+/, /g' file

Upvotes: 5

Cyrus
Cyrus

Reputation: 88989

Try this:

sed 's/, */, /g' filename

Upvotes: 1

Zulu
Zulu

Reputation: 9285

With GNU sed:

sed -re 's/,/, /g' -i your_file

  • -i your_file: The file to edit

Upvotes: 0

Related Questions