sed command to replace multiple spaces into single spaces
You can use awk
to solve this:
awk '{$0=tolower($0);$1=$1}1' test.txt
iihi hello hi
this is loga
sed 's/ \+/ /g' test.txt | tr [A-Z] [a-z]
or
sed 's/\s\+/ /g' test.txt | tr [A-Z] [a-z]
Good grief that was terse, because *
matches zero or more it inserted a space after every character, you want +
which matches one or more. Also I switched the order because in doing so you don't have to cat
the file.
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
.
Using tr
, the -s
option will squeeze consecutive chars to a single one:
tr -s '[:space:]' < test.txt
iiHi Hello Hi
this is loga
To downcase as well: tr -s '[:space:]' < test.txt | tr '[:upper:]' '[:lower:]'