Linux 如何从给定的文本文件中删除所有空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9953448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:31:30  来源:igfitidea点击:

How to remove all white spaces from a given text file

linuxbashsed

提问by Lunar Mushrooms

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 sedfor this purpose.

我想从给定的文本文件中删除所有空格。是否有任何可用的 shell 命令?或者,如何sed用于此目的。

I want something like below:

我想要像下面这样的东西:

$ cat hello.txt | sed ....

$ 猫你好.txt | sed ....

I tried this : cat hello.txt | sed 's/ //g'.But it removes only spaces, not tabs.

我试过这个:cat hello.txt | sed 's/ //g'。但它只删除空格,而不是制表符。

采纳答案by Paulo Scardine

$ 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 " \t\n\r" 

You can also use the character classes defined by tr (credits to htompkinscomment):

您还可以使用 tr 定义的字符类(归功于htompkins评论):

cat file.txt | tr -d "[:space:]"

For example, in order to wipe just horizontal white space:

例如,为了只擦除水平空白区域:

cat file.txt | tr -d "[:blank:]"

回答by Jerry Coffin

hmm...seems like something on the order of sed -e "s/[ \t\n\r\v]//g" < hello.txtshould be in the right ballpark (seems to work under cygwin in any case).

嗯......似乎sed -e "s/[ \t\n\r\v]//g" < hello.txt应该在正确的范围内(似乎在任何情况下都可以在 cygwin 下工作)。

回答by keyser

Try this:

尝试这个:

sed -e 's/[\t ]//g;/^$/d' 

(found here)

(在这里找到)

The first part removes all tabs (\t) and spaces, and the second part removes all empty lines

第一部分删除所有制表符 ( \t) 和空格,第二部分删除所有空行

回答by Umae

I think you may use sed to wipe out the space while not losing some infomation like changing to another line.

我认为您可以使用 sed 来清除空间,同时不会丢失一些信息,例如更改为另一行。

cat hello.txt | sed '/^$/d;s/[[:blank:]]//g'

回答by Lucie G

Much simpler to my opinion:

我的看法要简单得多:

sed -r 's/\s+//g' filename

回答by glenn Hymanman

If you want to remove ALLwhitespace, even newlines:

如果要删除所有空格,甚至是换行符:

perl -pe 's/\s+//g' file

回答by Gorton Fishman

This is probably the simplest way of doing it:

这可能是最简单的方法:

sed -r 's/\s+//g' filename > output
mv ouput filename

回答by user3653982

Try this:

尝试这个:

tr -d " \t" <filename

See the manpage for tr(1) for more details.

有关更多详细信息,请参阅 tr(1) 的联机帮助页。

回答by mminski

Easiest way for me ->

对我来说最简单的方法->

        echo "Hello my name is Donald" | sed  s/\ //g

回答by Agnibesh Chauhan

Dude, Just python test.py in your terminal.

伙计,只需在终端中使用 python test.py 即可。

f = open('/home/hduser/Desktop/data.csv' , 'r')

x = f.read().split()
f.close()

y = ' '.join(x)
f = open('/home/hduser/Desktop/data.csv','w')
f.write(y)
f.close()