如何从给定的文本文件中删除所有空格 [英] How to remove all white spaces from a given text file
本文介绍了如何从给定的文本文件中删除所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从给定的文本文件中删除所有空格.是否有任何可用的 shell 命令?或者,如何为此使用 sed
.
I want to remove all the white spaces from a given text file. Is there any shell command available for this ? Or, how to use sed
for this purpose.
我想要如下内容:
$ cat hello.txt |sed ....
$ cat hello.txt | sed ....
我试过了:cat hello.txt |sed 's///g'
.但它只删除空格,而不是制表符.
I tried this : cat hello.txt | sed 's/ //g'
.But it removes only spaces, not tabs.
推荐答案
$ man tr
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
DESCRIPTION
Translate, squeeze, and/or delete characters from standard
input, writing to standard output.
为了擦除所有空格,包括换行符,您可以尝试:
In order to wipe all whitespace including newlines you can try:
cat file.txt | tr -d "
"
您还可以使用 tr 定义的字符类(归功于 htompkins 评论):
You can also use the character classes defined by tr (credits to htompkins comment):
cat file.txt | tr -d "[:space:]"
例如,为了只擦除水平空白:
For example, in order to wipe just horizontal white space:
cat file.txt | tr -d "[:blank:]"
这篇关于如何从给定的文本文件中删除所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文