We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

scurtis_1
scurtis_1 asked
on
Medium Priority
27,889 Views
Last Modified: 2013-12-26
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

Commented:
Hi scurtis_1,

sed 's/ */ /g' filename


Sunnycoder
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
As sunnycoder said, but make sure there is TWO SPACES before *

sed 's/<SPACE><SPACE>*/ /g' filename
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
being pedantic, whitespace  also contains newline and carriage return
Do you mean that too?

Author

Commented:
Initially I meant just space characters but I have now included all newline and carriage return characters for my particular problem.

scott
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.