移除多余空格
使用下面的指令,但书上讲的\+是赋予+特殊的含义,这个就不明白来,\不是转义字符吗?\+应该是是+失去特殊含义才对嗄,不清楚怎么回事,请大N解答
sed 's/[ ]\+/ /g'
有关转义字符的问题
- astolia
- 论坛版主
- 帖子: 6703
- 注册时间: 2008-09-18 13:11
Re: 有关转义字符的问题
因为sed的正则默认用的是Basic Regular Expression(BRE) 规则
代码: 全选
man sed
而BRE中+和其他一些字符并没有特殊意义,需要加\REGULAR EXPRESSIONS
POSIX.2 BREs should be supported, but they aren't completely because of
performance problems. The \n sequence in a regular expression matches
the newline character, and similarly for \a, \t, and other sequences.
代码: 全选
code grep
Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and )
lose their special meaning; instead use the backslashed versions \?,
\+, \{, \|, \(, and \).
- astolia
- 论坛版主
- 帖子: 6703
- 注册时间: 2008-09-18 13:11
Re: 有关转义字符的问题
另外,用-r选项可以让sed使用ERE,+就不需要加\了
更多BRE和ERE的故事,可以看这篇
http://blog.csdn.net/xiaotengyi2012/art ... ls/8255005
代码: 全选
$ echo "a b" | sed -r 's/[ ]+/ /g'
a b
http://blog.csdn.net/xiaotengyi2012/art ... ls/8255005