sed 命令将多个空格替换为单个空格 [英] sed command to replace multiple spaces into single spaces

查看:72
本文介绍了sed 命令将多个空格替换为单个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 sed 将文件中的多个空格替换为单个空格.

I tried to replace multiple spaces in a file to single space using sed.

但它会像下面这样拆分每个字符.请让我知道是什么问题...

But it splits each and every character like below. Please let me know what the problem is ...

$ cat test.txt
 iiHi Hello   Hi
this   is   loga

$

$ cat test.txt | tr [A-Z] [a-z]|sed -e "s/ */ /g"
 i i h i h e l l o h i
 t h i s i s l o g a 

推荐答案

您的 sed 命令做了错误的事情,因为它匹配零个或多个空格",这当然发生在每对字符之间!你需要 s/*//gs/+//g 而不是 s/*//g.

Your sed command does the wrong thing because it's matching on "zero or more spaces" which of course happens between each pair of characters! Instead of s/ */ /g you want s/ */ /g or s/ +/ /g.

这篇关于sed 命令将多个空格替换为单个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆