开发者

Regex to reduce the spaces in a file by 1

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开发者_如何学Go    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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜