Solved

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

Posted on 2004-03-24
6
27,622 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
0
Comment
Question by:scurtis_1
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10666990
Hi scurtis_1,

sed 's/ */ /g' filename


Sunnycoder
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 150 total points
ID: 10667001
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 10668652
As sunnycoder said, but make sure there is TWO SPACES before *

sed 's/<SPACE><SPACE>*/ /g' filename
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 4

Assisted Solution

by:Vinit Kain
Vinit Kain earned 100 total points
ID: 10670206
hi,

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

will replace multiple white space to single space

Vinit
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10675524
being pedantic, whitespace  also contains newline and carriage return
Do you mean that too?
0
 

Author Comment

by:scurtis_1
ID: 10676399
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
twoTwo  challenge 35 101
pre4 challenge 19 100
Line meaning 9 85
x-bar in Google Sheets 2 52
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question