tags:

views:

52

answers:

2

I've a file and I want to make the following changes in it:

Replace n consecutive spaces by n-1 spaces.

Example:

Input:

a b  c   e
fg j    ij k

Output:

ab c  e
fgj   ijk

How do I do it?

+5  A: 

s/ ( *)/$1/g

That is, replace a space followed by any number (including 0) of spaces, with the latter number of spaces. In essence, we're matching / +/ while capturing / */.

hobbs
+5  A: 

You can do it in sed as:

sed  -r -i 's/ ( *)/\1/g' in

The regex used / ( *)/ searches for a space followed by zero or more spaces. It remembers the zero or more spaces part and replaces the entire spaces by the remembered part.

codaddict