[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 27669
  • Last Modified:

SED: How to replace all occurances of multiple whitespace with a single whitespace?

Hi,

I have a file with a single line of text in it, but the thing generating it puts large chunks of spaces throughout the line. Is there a regex format I can use with SED to replace all of these whitespace chunks with a single space?

Thanks
Scott
0
scurtis_1
Asked:
scurtis_1
2 Solutions
 
sunnycoderCommented:
Hi scurtis_1,

sed 's/ */ /g' filename


Sunnycoder
0
 
sunnycoderCommented:
above will insert single space after each char ...

sed 's/  */ /g' filename

will replace one or more space characters with a single space character (notice extra space char after first / )
0
 
HamdyHassanCommented:
As sunnycoder said, but make sure there is TWO SPACES before *

sed 's/<SPACE><SPACE>*/ /g' filename
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Vinit KainCommented:
hi,

sed 's/[<space><tab>][<space><tab>]*/ /g' filename

will replace multiple white space to single space

Vinit
0
 
ahoffmannCommented:
being pedantic, whitespace  also contains newline and carriage return
Do you mean that too?
0
 
scurtis_1Author Commented:
Initially I meant just space characters but I have now included all newline and carriage return characters for my particular problem.

scott
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now