Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed command to replace multiple spaces into single spaces

Tags:

regex

shell

sed

I tried to replace multiple spaces in a file to single space using sed.

But it splits each and every character like below. Please let me know what the problem is ...

$ cat test.txt
 iiHi Hello   Hi
this   is   loga

$

$ cat test.txt | tr [A-Z] [a-z]|sed -e "s/ */ /g"
 i i h i h e l l o h i
 t h i s i s l o g a 
like image 263
logan Avatar asked Apr 08 '14 13:04

logan


1 Answers

Your sed command does the wrong thing because it's matching on "zero or more spaces" which of course happens between each pair of characters! Instead of s/ */ /g you want s/ */ /g or s/ +/ /g.

like image 53
John Zwinck Avatar answered Oct 25 '22 00:10

John Zwinck