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?
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?
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 / */
.
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.