我尝试使用sed将文件中的多个空格替换为单个空格.
但它会分割下面的每个角色.请让我知道问题是什么......
$ 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
Run Code Online (Sandbox Code Playgroud)
Joh*_*nck 18
你的sed命令做错了,因为它匹配"零或多个空格",这当然发生在每对字符之间!而不是s/ */ /g你想要s/ */ /g或s/ +/ /g.
gle*_*man 14
使用tr,该-s选项会将连续的字符挤压为单个字符:
tr -s '[:space:]' < test.txt
Run Code Online (Sandbox Code Playgroud)
iiHi Hello Hi
this is loga
Run Code Online (Sandbox Code Playgroud)
也是羽绒服: tr -s '[:space:]' < test.txt | tr '[:upper:]' '[:lower:]'
sed 's/ \+/ /g' test.txt | tr [A-Z] [a-z]
Run Code Online (Sandbox Code Playgroud)
或者
sed 's/\s\+/ /g' test.txt | tr [A-Z] [a-z]
Run Code Online (Sandbox Code Playgroud)
很遗憾,很简洁,因为*匹配零个或多个,它在每个字符后插入一个空格,您想要+哪个匹配一个或多个。我也改变了顺序,因为这样做你不需要cat文件。
| 归档时间: |
|
| 查看次数: |
31677 次 |
| 最近记录: |