user2074481
user2074481

Reputation: 59

Replacement of single space with sed without touching multiple spaces

Replacement of single space using sed without touching sequences of several spaces.

I have file that looks like this:

Host/Domain Summary: Message Delivery
 sent cnt  bytes   defers   avg dly max dly host/domain

 -------- -------  -------  ------- ------- -----------
    937     3293k       0    18.6 s    6.9 m  theory.tifr.res.in

    462     3740k       0     7.3 s    9.5 m  ncra.tifr.res.in

    286     1053k       4    20.4 s    1.3 h  gmail.com

Question:

I want to replace only single space with comma or without space but without touching other spaces. I have tried it with sed but it directly takes all space and replaces them with a comma.

Example: 18.6 s and 6.9 m having single space so that can only replace by value which we give in sed but others cannot.

How can it be done?

Upvotes: 2

Views: 538

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 755064

It seems like a funny thing to be doing, but this should do what you say you want to do:

sed 's/\([^ ]\) \([^ ]\)/\1,\2/g'

Replace a non-blank followed by a blank followed by another non-blank with the first non-blank, a comma, and the second non-blank. Repeat for each single blank in the line.

Upvotes: 3

Related Questions