[英]How can I keep the string of one space only and replace all the other spaces using sed?
I have a data file (1.data) which contains the following: 我有一个数据文件(1.data),其中包含以下内容:
USSR 8649 BS275 Asia Cananda 3852 BS25 North America
What I want is to keep the string of only one space and replace all the other spaces with : 我想要的是仅保留一个空格的字符串,并用:替换所有其他空格:
My sed code: 我的sed代码:
sed 's/ * */:/g' 1.data
:USSR:8649:BS275:Asia
:Cananda:3852:BS25:North America
That works perfect. 完美的作品。
Since I know A+ matches A, AA, AAA, AAAA and so on, I tried another regular expression. 由于我知道A +匹配A,AA,AAA,AAAA等,因此我尝试了另一个正则表达式。
sed 's/ * +/:/g' 1.data
USSR 8649 BS275 Asia
Cananda 3852 BS25 North America
However, it does not work. 但是,它不起作用。 Nothing changes. 没有什么变化。
What mistake did I make? 我犯了什么错误?
添加选项-E
(从正则表达式切换为扩展正则表达式)或将+
替换为\\+
。
sed
would treat "+" as a normal character if no -r
option is added. 如果未添加-r
选项,则sed
会将“ +”视为普通字符。 And "+" meta-character would match to one or more after -r: extended regular expressions
is added. 在-r: extended regular expressions
之后,“ +”元字符将匹配一个或多个-r: extended regular expressions
添加了-r: extended regular expressions
。 So your command can be modified as, 因此您的命令可以修改为
sed -r 's/[[:space:]]+/:/g' 1.data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.