Replace all occurences of two spaces after the end of a sentence with just one space

Your sed command 's/^ $/^$/' won't do what you want. It just replace all lines contains one space with a line contain ^$.

Depend on what characters mark end of sentence, you can do:

sed -e 's/\([.?!]\) \{2,\}/\1 /g' <file

This will replace 2 or more spaces after ., ? or ! with one space only.


 sed 's/\.   */. /g' < file

replace dot followed by two or more spaces with dot followed by a single space.


This is what you might be looking for,

tr -s " " <filename

Sample,

$ echo "This is the output.  Hello Hello" | tr -s "[:blank:]"
This is the output. Hello Hello

Using sed,

$ echo "This is the output.  Hello Hello" | sed 's/\. \+/. /g'
$ echo "This is the output.  Hello Hello" | sed 's/\. \{1,\}/. /g'
This is the output. Hello Hello